C ++ parameter of non-piggy const char * pattern

Let's say that

template <const char*>
struct A{};

// static storage
const char a[] = "asd";
const char* p = "asd";

This description

A<a>{};

suitable for compiler. And this is understandable - the array adecays to a pointer to the first element. But if we create an instance awith the pfollowing

A<p>{};

the compiler gives an error message:

error: non-type char * 'type template argument is not a constant expression

Why doesn't the standard allow you to specify a named variable of a type const char*or just a string literal "asd", which is btw lvalue itself, as a template argument?

+4
source share
2 answers

a , , , .

p , , , , ( ) . p , p .


, , , - , . ( ).

+9

, A<p>{}; , , p

const char* p = getP();

getP() , , .

0

Source: https://habr.com/ru/post/1598612/


All Articles