考慮下面的代碼:爲什麼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)。 –
你真的叫過「方法」嗎?編譯器不會爲未執行的模板函數生成代碼。 –