gpt4 book ai didi

c++ - 这是我的代码中的错误还是 g++ 对 -Weffc++ 的分析中的错误?

转载 作者:行者123 更新时间:2023-11-28 05:50:31 25 4
gpt4 key购买 nike

我收到 -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 测试。

更新

添加了一个附加类以提供实现示例。警告本身是正确的,但我认为我不同意警告出现在纯虚函数上,因为它本来是另一个类的接口(interface)。

@Frerich Raabe 已就 g++ 认为我违反了 Effective C++ 设定的规则的原因提供了必要的澄清,我已经接受了这个答案。

为了消除警告,我添加了以下内容:

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
virtual class_type& operator++() = 0;
#pragma GCC diagnostic pop

最佳答案

编译器是正确的。你的operator++重新实现应返回与 *this 相同类型的值,即 some_class<...> .事实上,operator++() 的许多实现结束于

return *this;

请参阅 this answer 中标记为“一元算术运算符”的部分进行更详细的讨论。

关于c++ - 这是我的代码中的错误还是 g++ 对 -Weffc++ 的分析中的错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35364576/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com