简体   繁体   English

使用clang ++和g ++,双嵌套模板类中的c ++ static int def失败

[英]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.

相关问题 编译模板类因g ++或clang ++失败 - Compile template class failed by g++ or clang++ C ++:模板化代码可使用clang ++编译并正常运行,但对于g ++则失败 - C++: Templated code compiles and runs fine with clang++, but fails with g++ C ++朋友函数模板重载和SFINAE在clang ++,g ++,vc ++中的不同行为(C ++ 14模式) - C++ friend function template overloading and SFINAE different behaviors in clang++, g++, vc++ (C++14 mode) 具有已删除“一般”情况的专用模板功能无法使用g ++ &lt;= 4.8.0和clang ++进行编译 - Specialized template function with deleted “general” case fails to compile with g++ <=4.8.0 and clang++ g ++和clang ++在模板类中定义的朋友模板函数的不同行为 - g++ and clang++ different behaviour with friend template function defined inside a template class 在类中定义的友元函数模板是否可用于查找? clang ++和g ++不同意 - Is a friend function template defined in the class available for lookup? clang++ and g++ disagree g ++和clang ++使用静态成员的递归初始化的不同行为 - g++ and clang++ different behaviour with recursive initialization of a static member 这是 g++ 或 clang++ 中的错误 - Is this a bug in g++ or clang++ 程序使用clang ++进行编译,但是g ++耗尽了RAM并失败 - Program compiles using clang++, but g++ exhausts the RAM and fails g ++和clang ++使用变量模板和SFINAE的不同行为 - g++ and clang++ different behaviour with variable template and SFINAE
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM