C++ compiler does not check if a method exists in template class

Template class methods are not instantiated until they are used. Once you try calling promote() or even get its address like this &Val<int>::promote then you'll get an error.

From the C++ standard:

§ 17.8.1.10 An implementation shall not implicitly instantiate a function template, a variable template, a member template, a non-virtual member function, a member class, a static data member of a class template, or a substatement of a constexpr if statement (9.4.1), unless such instantiation is required.


Templates have always worked this way, principally to facilitate their use.

Because Val<int>(4).val(); doesn't call promote, that function is not compiled for your particular instantiation of that template so the compiler does not issue a diagnostic.

Many metaprogramming techniques depend on this behaviour.