dre*_*rel 5 c++ language-lawyer list-initialization template-variables c++17
我正在尝试实现具有以下特征的模板化C数组:
// template definition
template< int a, int b > constexpr int arr[] = { 1 };
// partial specialization, works ok
template< int b > constexpr double arr<0, b>[] = { 2.0 };
// full specialization, compile error in MSVC, ok in g++
template< > constexpr float arr<1, 0>[] = { 3.0f };
Run Code Online (Sandbox Code Playgroud)
我将Visual Studio 2017和MSVC编译器与C ++标准设置为C ++ 17一起使用,并且编译器抱怨了C2133: 'arr<1,0>': unknown size,因此将大小添加1到完整的专业化解决了该错误。但是,它会在带有-pedantic标志的Ubuntu g ++ 8.1.0下进行编译。
我认为,对函数和类的完全专业化的行为就好像定义了非模板版本一样,因此我想这也应适用于变量模板,并且上面的完全专业化可以等同于(名称除外)
constexpr float arr_with_a1_and_b0[] = { 3.0f };
Run Code Online (Sandbox Code Playgroud)
这对我来说似乎很有效,因为应该从列表初始化(聚合初始化)中推导出大小。
我的问题是:上面的代码是否有效的C ++?哪个编译器正确?
这是由于 MSVC 编译器的错误造成的:https://developercommunity.visualstudio.com/t/compiler-error-c2133-unknown-size-for-constant-tem/228098。从带有 Visual Studio 16.10.1 的 MSVC 开始,此问题已得到修复。