I am learning about non-type template parameters using C++ Primer 5th edition and came to know that:
A nontype parameter may be an integral type, or a pointer or (lvalue) reference to an object or to a function type. An argument bound to a nontype integral parameter must be a constant expression.
Now to further clear my concept and confirm that I've understood the topic correctly, I wrote the following program that compiles with msvc but not with clang. Demo
#include <iostream>
template<char N> class S
{
public:
S()
{
std::cout << "char template" << std::endl;
}
};
int main()
{
S<128> s1; //MSVC compiles this but gcc and clang rejects
}
So my question is which compiler is right here(if any). I tried to look this up in the book itself but I couldn't figure out whether the program is valid according to the c++ standard. MSVC compiles this while gcc and clang says:
error: non-type template argument evaluates to 128, which cannot be narrowed to type 'char'