'std::string': illegal type for non-type template parameter

The code I'm trying to compile is this

1
2
3
4
5
6
template<typename T, T default_value = T{}>
struct Vec
{


};


This code is taken from Bjarne Stroustrup's "The C++ Programming Language" book at page 725

What this code does is obvious... but when I try to instantiate the struct with a std::string the error in the title appears

 
Vec<string> s;


Error: 'std::string': illegal type for non-type template parameter

Why?

---

I wanted to try this code because on Bjarne Stroustrup's book he makes this example, and he writes the following:

 
Vec<string>   // default_value is int*{} that is, nullptr 



I said: WHAT?

That's a string, why should T be evaluated as an int*?

So i compiled the code myself and got this error...
Error: 'std::string': illegal type for non-type template parameter

Template non-type parameters cannot be objects of class type. See, e.g.,: http://en.cppreference.com/w/cpp/language/template_parameters

I'm looking at my copy (the 4e) and I can't find any mention of this on page 725. I expect that you have the fourth edition too, thanks to mention of nullptr. With that in mind, I checked the errata (at http://www.stroustrup.com/4th_printing3.html ) and found nothing relevant.

Various text searches (I have a PDF copy) similarly yield nothing. In particular, grepping for Vec<string> yields exactly one result:
Vec<string> c22; // default_value is string{}; that is, ""

Where exactly does the issue appear? What book do you have?
Last edited on
Yes I have the fourth edition too

I took some pictures of it, link to see them >> http://imgur.com/a/xQshl

Very strange situation...

Either way... the code should work... my code is the same as the one in the book...

Template non-type parameters cannot be objects of class type


In this case the non-type parameter is default_value that gets evaluated as string{}
Since string{} is an object of a class-type, it is not allowed.

Correct?

But... why then the book reports this example if it is not allowed?
Last edited on
But... why then the book reports this example if it is not allowed?

It's a typo. (Or both of us are misunderstanding it.)
Last edited on
It's a typo.


Do you have these typos as well?

I see (and you can see too with the screenshots) that Bjarne uses the string class as non-type parameter twice

 
Vec<string,"">


Lots of typos?
You can use at least the following types as template non-type parameters [1, 2]:
- std::nullptr_t
- integral type;
- lvalue reference type (to object or to function);
- pointer type (to object or to function);
- pointer to member type (to member object or to member function);
- enumeration type.

String literals are not usable as template non-type arguments[3, 4]:; not even lvalue expressions with linkage satisfying LiteralType[4] are allowed[1]
Evidence:
http://coliru.stacked-crooked.com/a/9c72d6ed409a49e1

because string literals have no linkage - there is no name to link. In my copy of the book, this example is given (pp. 724):
1
2
3
X<int,"BMW323Ci"> x1; // error : string literal as template argument
char lx2[] = "BMW323Ci";
X<int,lx2> x2; // OK: lx2 has external linkage 


It seems like Stroustrup forgot that string literals are unusable in the context of a template non-type argument, or that behavior changed late during the standardization process.

1: http://en.cppreference.com/w/cpp/language/template_parameters#Template_non-type_arguments
2: http://eel.is/c++draft/temp.param#4
3: http://eel.is/c++draft/temp.arg.nontype#2
4: http://en.cppreference.com/w/cpp/concept/LiteralType

Do you have these typos as well?

Some: I have
Vec<string,"fortytwo"> c2; and
Vec<string,""> c2; and
Vec<string> c22;
And those are the only errors I spot. The strange thing is that Stroustrup mentions this exact thing is disallowed right before making the error. Perhaps someone else should check me on this.
Last edited on
You are not the only one who pointed out this issue:
https://stackoverflow.com/questions/20066104/is-a-string-literal-not-acceptable-as-a-template-argument-in-c/20066609
It seems there's a wrong example in that book.
I don't have those examples in my book, check my screenshot of page 725 >> http://imgur.com/a/xQshl

It doesn't seem to be the same...
Topic archived. No new replies allowed.