c – 如果我的模板化类型首先作为lambda参数出现,MSVC会引发一个
发布时间:2020-12-16 06:53:38 所属栏目:百科 来源:网络整理
导读:考虑以下代码: #define SOLUTION 0template class Tconstexpr int one = 1;template class Tstruct A { static constexpr int o = oneAT; void call() { static_assert(oneAT == 1,"Failure"); }};int main() {#if SOLUTION Aint object;#endif [](Aint a)
考虑以下代码:
#define SOLUTION 0 template <class T> constexpr int one = 1; template <class T> struct A { static constexpr int o = one<A<T>>; void call() { static_assert(one<A<T>> == 1,"Failure"); } }; int main() { #if SOLUTION A<int> object; #endif [](A<int> a) { a.call(); }; return 0; } 无论SOLUTION定义的值如何,它都会成功构建here on ideone. 现在,我知道这段代码几乎没有实际意义,但那是因为我正在努力寻找这种奇怪行为的最小工作示例.如果我使用最新的Visual Studio 2017(平台工具集v141,_MSC_VER = 1910)构建它,我会收到以下错误: 1>source.cpp(11): error C2131: expression did not evaluate to a constant 1>source.cpp(11): note: failure was caused by a read of an uninitialized symbol 1>source.cpp(11): note: see usage of 'one<A<int>>' 1>source.cpp(10): note: while compiling class template member function 'void A<int>::call(void)' 1>source.cpp(21): note: see reference to function template instantiation 'void A<int>::call(void)' being compiled 1>source.cpp(21): note: see reference to class template instantiation 'A<int>' being compiled 有趣的是,当我为解决方案宏添加1时,它会成功构建.唯一的区别是未使用的A< int>宾语;在编译器到达lambda定义之前,在main的范围内定义变量. 如果在main之前我定义了这样的函数,那也很好: void f(A<int>& a) { a.call(); } 事实上,它与变量定义一样可以很好地解决问题. 它是编译器错误还是违反标准的上述代码块? 解决方法
引用
Ajay:
我也在我的机器上确认了这种行为.这意味着相关的更正存在并将很快引入生产版本本身,因此我认为这个错误值得进一步注意. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |