constexpr most often allowed in templates, because at the time of template definition it is not known at all whether the element satisfies constexpr requirements. When a constexpr template member is constexpr , it is determined during the instantiation of the template whether constexpr suitable, and if not, it is silently discarded.
Considering
template <typename T> struct test { T val; constexpr test(T val) : val(val) { } };
you may have
constexpr test<int> i = 3;
since with T = int constructor satisfies the requirements of constexpr , but you cannot have
constexpr test<string> s = "";
because this constructor does not meet the requirements of constexpr .
It is not difficult to create an instance of test<string> , because this will greatly limit the possibility of using constexpr in templates.
From the standard (C ++ 11):
7.1.5 constexpr specifier [dcl.constexpr]
6 If the specialized specialization of the constexpr function constexpr or member function of the class template does not satisfy the requirements for the constexpr function or constexpr constructor, this specialization is not a constexpr or constexpr . [...]
source share