mercredi 11 mars 2015

pointer non-type template parameter

I don't really understand why the code below does not compile:



template<const char*>
struct Foo{};

constexpr const char s1[] = "test1";
constexpr const char* const s2 = "test2";

int main()
{
Foo<s1> foo1; // ok
// Foo<s2> foo2; // doesn't compile
}


Uncommenting the last line in main() makes g++ and clang++ emit the errors




error: 's2' is not a valid template argument because 's2' is a
variable, not the address of a variable


and




error: non-type template argument for template parameter of
pointer type 'const char *' must have its address taken


respectively.


My questions are:



  1. Why is s1 instantiation OK and s2 not?

  2. Is there any sane situation where such pointer non-type template parameter is of any use?


Aucun commentaire:

Enregistrer un commentaire