5

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?

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
drel
  • 165
  • 3
  • 8
  • Do you define specialization in class ? There was a defect in standard: http://open-std.org/JTC1/SC22/WG21/docs/cwg_defects.html#727 – NN_ Feb 28 '19 at 07:11
  • @NN_ Thanks for the info! No, the specialization is defined in the namespace scope. – drel Feb 28 '19 at 07:19
  • Seem to be some issues with array specializations in MSVC. See this reported bug: https://developercommunity.visualstudio.com/content/problem/176842/c-modules-array-specializations-dont-work.html – P.W Feb 28 '19 at 07:28
  • @P.W I've just found the thread that exactly describes the problem: https://developercommunity.visualstudio.com/content/problem/228098/compiler-error-c2133-unknown-size-for-constant-tem.html – drel Nov 22 '19 at 20:02

1 Answers1

2

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.

drel
  • 165
  • 3
  • 8