c++language-lawyerc++17list-initializationtemplate-variables

Should template variable C-array full specialization specify array size?


I am trying to implement a templated C-array with specialization as the following:

// 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 };

I am using MSVC compiler with Visual Studio 2017, with the C++ standard set to C++17, and the compiler complains that C2133: 'arr<1,0>': unknown size, so adding the size 1 to the full specialization resolves the error. However, it compiles under Ubuntu g++ 8.1.0 with -pedantic flag on.

In my opinion, full specialization on functions and classes acts as if a non-template version is defined, so I guess this should also apply to the variable template, and the full specialization above could be equivalent to (except for the name)

constexpr float arr_with_a1_and_b0[] = { 3.0f };

which looks pretty valid to me, since the size should be deduced from the list-initialization (aggregate-initialization).

My question is: Is the code above valid C++? Which compiler is correct?


Solution

  • This was due to a bug with MSVC compiler: https://developercommunity.visualstudio.com/t/compiler-error-c2133-unknown-size-for-constant-tem/228098. As of MSVC with Visual Studio 16.10.1, this problem has been fixed.