C++ `在非静态数据成员“”之前需要构造函数-我是否遇到c++;核心版1360,带有“boost::variant”?
使用此代码C++ `在非静态数据成员“”之前需要构造函数-我是否遇到c++;核心版1360,带有“boost::variant”?,c++,c++17,inner-classes,boost-variant,C++,C++17,Inner Classes,Boost Variant,使用此代码 #include <boost/variant.hpp> #include <string> struct Outer { struct StateA { std::size_t index = std::string::npos; }; struct StateB {}; using State = boost::variant<StateA, StateB>; struct
#include <boost/variant.hpp>
#include <string>
struct Outer
{
struct StateA
{
std::size_t index = std::string::npos;
};
struct StateB {};
using State = boost::variant<StateA, StateB>;
struct Inner
{
State state = StateB{};
};
};
int main() {
Outer o;
(void)o;
}
#包括
#包括
结构外部
{
结构状态A
{
std::size\u t index=std::string::npos;
};
结构StateB{};
使用State=boost::variant;
结构内部
{
State-State=StateB{};
};
};
int main(){
外o;
(c)o;
}
我得到以下编译错误
/usr/include/boost/variant/variant.hpp:1301:17: required from ‘class boost::variant<Outer::StateA, Outer::StateB>’
inner_class.cpp:18:30: required from here
/usr/include/boost/type_traits/has_nothrow_constructor.hpp:27:84: error: constructor required before non-static data member for ‘Outer::StateA::index’ has been parsed
template <class T> struct has_nothrow_constructor : public integral_constant<bool, BOOST_HAS_NOTHROW_CONSTRUCTOR(T)>{};
/usr/include/boost/variant/variant.hpp:1301:17:必须来自“class boost::variant”
内部课程。cpp:18:30:从这里开始需要
/usr/include/boost/type_traits/has_nothrow_构造函数。hpp:27:84:错误:解析'Outer::StateA::index'的非静态数据成员之前需要构造函数
模板结构具有\u nothrow\u构造函数:公共整数\u常量{};
提到这个问题,我似乎触及了核心问题,但我想检查一下我的理解
具体地说,我在打吗
当前文本中未指定的顺序依赖项
在
内部中初始化变体的类型定义时?或者内部struct
s是否还有其他原因导致此编译错误?这不取决于(发现的)任何内容constepr
,因此它与CWG1360不同,但它肯定是相似的,因为它利用了不可能同时让嵌套类成为其包含类的完整类上下文,并且让嵌套类在包含类中比其定义更晚完成的情况。也许最好的轨迹是,对于这个轨迹,一般(长期)的建议是,类中的实际依赖应该通过类似实例化的机制来跟踪;像您这样的示例要求跨类进行这样的处理。我不需要struct-Inner
来获得编译错误,也就是说,如果我有一个成员变量Outer::state
以相同的方式初始化,并删除struct-Inner
的代码,我会得到相同的编译错误,谢谢。当CWG2335说“在类完成之前,不应该对compute_right的函数体进行求值”时,您知道这个求值顺序是在哪里定义的吗?@crawgar23:这(由)[class.mem]/6]()暗示),表示类在那里被认为是完整的(因此“正向查找”)-实际上,可能会发生延迟查找)。