tdenniston
tdenniston

Reputation: 3519

MSVC error with constexpr array as template non-type parameter

I'm trying to understand if what I'm seeing is an MSVC compiler bug or a misunderstanding on my part. I'm trying to use a simple compile-time string literal template parameter, like so:

constexpr const char teststr[] = "teststr";

template <const char *N>
struct Test {
  static constexpr const char *name = N;
};

using TEST = Test<teststr>;

However, MSVC reports that the teststr parameter is not a compile-time expression: error C2975: 'Test': invalid template argument for 'N', expected compile-time constant expression (on the using TEST line).

Is this my mistake, or a compiler bug? I am using Visual Studio 2017 version 15.1.

Upvotes: 2

Views: 213

Answers (2)

xskxzr
xskxzr

Reputation: 13040

It is a compiler bug.

In addition, it is a C++11 feature to permit addresses of objects with internal linkage in template arguments, not C++14.

Upvotes: 1

Sid S
Sid S

Reputation: 6125

Is it important that name is static ?

This compiles with MSVC 2015:

char teststr[] = "teststr";

template <const char *N>
struct Test
{
    const char *name = N;
};

using TEST = Test<teststr>;

Upvotes: 0

Related Questions