Consider the following code:
template <typename T>
class C2 {
public:
T method() { }
int method2() { }
};
Compiling it with g++ -Wall -c -pedantic gives me the following warning:
test.cpp: In member function ‘int C2<T>::method2()’:
test.cpp:4:29: warning: no return statement in function returning non-void [-Wreturn-type]
Which is expected. The strange thing is that method() isn’t returning anything either. Why doesn’t that generate a warning, since instantiating C2 with T = int makes calls to both methods equally dangerous?
If you say
T = void, then noreturnstatement is needed.Just because you can use your template in a way that’s broken doesn’t mean you have to, and the compiler may be giving you the benefit of the doubt.
Also remember that member functions of a class template are only instantiated if and when used. So the way to actually cause an error is to have
C2<char> x; x.method();, and that does indeed produce a warning.