考虑下面的代码:为什么G ++不会为模板方法产生警告而不返回任何内容?
template <typename T>
class C2 {
public:
T method() { }
int method2() { }
};
与g++ -Wall -c -pedantic
编译它给了我以下警告:
test.cpp: In member function ‘int C2<T>::method2()’:
test.cpp:4:29: warning: no return statement in function returning non-void [-Wreturn-type]
这是预期。奇怪的是method()
也没有返回任何东西。为什么不会产生警告,因为使用T = int
实例化C2
会使这两种方法的调用同样危险?
你在编译时在某处实例化了'C2'吗? – Pubby
[Works here](http://liveworkspace.org/code/3ea9d586eb0ce16cff833f15be02b0e7)。 –
你真的叫过“方法”吗?编译器不会为未执行的模板函数生成代码。 –