[英]c++ static int def in doubly nested template class fails with clang++ and g++
With following code: 使用以下代码:
struct my_symbols {
enum class syms { symb_0_0, symb_0_1 };
};
template
< typename SymbolEnums
>
struct outer {
using syms_0 = typename SymbolEnums ::syms;
template <syms_0 AnSym0, int Int>
struct inner {
static int const val;
};
};
template
< typename SymbolEnums
>
template
< typename outer<SymbolEnums>::syms_0 AnSym0
, int Int
>
int const outer<SymbolEnums>::inner<AnSym0, Int>::val = Int;
int main() {
return
outer<my_symbols>::
inner<my_symbols::syms::symb_0_1, 1>::val;
}
I get, with gcc5.2.0: 我得到了,用gcc5.2.0:
template definition of non-template 'int outer_tmpl::inner_tmpl::val' val=Int;
非模板的模板定义'int outer_tmpl :: inner_tmpl :: val'val = Int; ^
^
and with clang3.8.0, I get: 和clang3.8.0,我得到:
nested name specifier 'outer_tmpl::inner_tmpl::' for declaration does not refer into a class, class template or class template partial specialization val=Int;
用于声明的嵌套名称说明符'outer_tmpl :: inner_tmpl ::'不引用类,类模板或类模板部分特化val = Int; ^
^
How can I correct the code? 我该如何更正代码?
TIA. TIA。
I do not know exactly why the posted code does not work. 我不知道为什么发布的代码不起作用。 I suspect the answer lies deep within the C++ Standard name lookup rules, which are incredibly complicated -- assuming, of course, that this is not actually a compiler bug.
我怀疑答案在C ++标准名称查找规则的深处,这些规则非常复杂 - 当然,假设这实际上不是编译器错误。 Someone else here probably knows.
这里的其他人可能都知道。
The following "equivalent" examples compile on GCC 5.2.0 and Clang 3.8. 以下“等效”示例在GCC 5.2.0和Clang 3.8上编译。 They may or may not be suitable for your situation.
它们可能适合您的情况,也可能不适合您的情况。
Example 1: Use constexpr
and in-class initialization 示例1:使用
constexpr
和类内初始化
struct my_symbols {
enum class syms { symb_0_0, symb_0_1 };
};
template<typename SymbolEnums>
struct outer {
using syms_0 = typename SymbolEnums::syms;
template <syms_0 AnSym0, int Int>
struct inner {
static constexpr int val = Int;
};
};
int main() {
return outer<my_symbols>::inner<my_symbols::syms::symb_0_1, 1>::val;
}
Example 2: Tweak the nested template type parameter 示例2:调整嵌套模板类型参数
struct my_symbols {
enum class syms { symb_0_0, symb_0_1 };
};
template<typename SymbolEnums>
struct outer {
using syms_0 = typename SymbolEnums::syms;
template <syms_0 AnSym0, int Int>
struct inner {
static int const val;
};
};
template<typename SymbolEnums>
template<typename SymbolEnums::syms AnSym0, int Int>
// ^^^^^^^^^^^^^^^^^^^^^^^^^^
int const outer<SymbolEnums>::inner<AnSym0, Int>::val = Int;
int main() {
return outer<my_symbols>::inner<my_symbols::syms::symb_0_1, 1>::val;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.