The following code fails to compile:
// template<class>
struct S {
int g() const {
return 0;
}
constexpr int f() const {
return g();
}
};
int main()
{
S /*<int>*/ s;
auto z = s.f();
}
GCC, for example, complains: error: call to non-constexpr function ‘int S::g() const’. This is perfectly reasonable. But if I turn S into a template, the code compiles (checked with MSVC 15.3, GCC 7.1.0, clang 4.0.1).
Why? Does constexpr has any special meaning in class templates?
As far as I understand it, this code is incorrect, but the standard does not require that compilers produce an error (why?).
Swas a template class that was derived from different base classes depending on a template parameter.g()'s were in bases classes. Then I madef()constexprand madeg()constexpronly in some bases classes (whereconstexprness made sense). I was perplexed to see no compiler error whenSwas derived from a base class with non-constexprg(). Hence, the question. - Evg