我收到了由-WeffC++引發的警告,這似乎是錯誤的。我可以用一個第二雙眼睛來確認:這是我的代碼或g ++分析-WeffC++中的錯誤嗎?
template<template<class> class CLASS_TYPE, typename T>
class some_class
{
typedef CLASS_TYPE<T> class_type;
public:
virtual ~some_class() {};
virtual class_type& operator++() = 0;
};
template<typename T>
class other_class
:
public some_class<other_class, T>
{
public:
virtual ~other_class() {};
other_class<T>& operator++() {
return *this;
};
};
int main() {
return 0;
}
的警告是:
main.cpp:8:39: warning: prefix ‘some_class<CLASS_TYPE, T>::class_type& some_class<CLASS_TYPE, T>::operator++()’ should return ‘some_class<CLASS_TYPE, T>&’ [-Weffc++]
virtual class_type& operator++() = 0;
測試與g++ (GCC) 4.9.3
。
UPDATE
加入另外的類來提供實現示例。警告是正確本身,但我認爲我的分歧是警告出現在純虛擬函數上,因爲它意味着要成爲另一個類的接口。
@Frerich Raabe已經提供了必要的解釋,爲什麼g ++認爲我違反了Effective C++設置的規則,並且我接受了這個答案。
要消除我已經添加了以下警告:
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
virtual class_type& operator++() = 0;
#pragma GCC diagnostic pop
'CLASS_TYPE'與'some_class '是同一類型嗎?我期望'some_class '被返回。 –
NathanOliver
您要求確認編譯器報告該代碼的警告? [確認](http://coliru.stacked-crooked.com/a/8bbd9555da6f5496)。你有理由相信編譯器是錯的嗎?你還沒有說明。 –
增加了額外的說明。 – Zhro