迂腐GCC警告:键入函数的返回类型修饰符(Pedantic gcc warning: type qu

2019-06-26 01:19发布

当我编译的C ++代码与GCC 4.3的第一次,(在已成功与4.1没有警告,4.0%,3.4与编译它-Wall -Wextra我突然接到了一堆表格的错误选项) warning: type qualifiers ignored on function return type

考虑temp.cpp

class Something
{
public:
    const int getConstThing() const {
        return _cMyInt;
    }
    const int getNonconstThing() const {
        return _myInt;
    }

    const int& getConstReference() const {
        return _myInt;
    }
    int& getNonconstReference() {
        return _myInt;
    }

    void setInt(const int newValue) {
        _myInt = newValue;
    }

    Something() : _cMyInt( 3 ) {
        _myInt = 2;
    }
private:
    const int _cMyInt;
    int _myInt;
};

运行g++ temp.cpp -Wextra -c -o blah.o

temp.cpp:4: warning: type qualifiers ignored on function return type
temp.cpp:7: warning: type qualifiers ignored on function return type

谁能告诉我什么,我做错了违反C ++标准? 我想,通过值返回时,领先的const是多余的,但我无法理解为什么有必要产生与它警告。 是否还有其他地方,我应该离开关常量?

Answer 1:

它不违反标准。 这就是为什么他们的警告 ,而不是错误

事实上你是对的-领先的const是多余的。 编译器警告你,因为你已经添加的代码,在其他情况下可能意味着什么,但在这种情况下意味着什么,并希望确保系统会在您的返回值变成毕竟是修改以后无法失望。



Answer 2:

编译使用Boost.ProgramOptions一些代码时,我遇到了这个警告。 我用-Werror所以警告是杀死我的身材,但因为报警的来源是加速我无法摆脱它的深度通过修改我的代码。

多挖掘后,我发现禁用警告的编译器选项:

-Wno-ignored-qualifiers

希望这可以帮助。



Answer 3:

返回一个恒定值才有意义,当你返回一个引用或指针(在这种情况下指针常量,而不是一个常量指针),因为来电者能够修改引用(指向)的价值。

在代码的另一种意见不相关的问题:我认为这是最好使用一个二传手,而不是

int& getNonconstReference() {
    return _myInt;
}

这将应该是:

void setMyInt(int n) {
  _myInt = n;
}

此外,它也没用const引用返回一个int。 应当把它用于一个更大的对象,其复制或移动更贵。



Answer 4:

有了这个

struct Foo { Foo(int) {} operator bool() { return true; } };

然后

Foo some_calculation(int a, int b) { Foo result(a + b); /*...*/ return result; }

这个例子

if (some_calculation(3, 20) = 40) { /*...*/ }

编译没有警告。 当然,这是罕见的。 但是,是不是使得人们很难做的事情错了const正确性? 而随着人们尝试的事情,但预期是错误的,返回类型应该声明为const。 和:G ++发出警告忽略了分类,但不会将其忽略。 我认为,该警告是关于采取复制,而忽略他们的副本常量分类的用户。 但是,这不应该是一个警告,因为这绝对是正确的行为。 它是有道理的做到这一点。



Answer 5:

不应该只允许-pedantic严格遵守ISO标准? 根据课程的-std = ...



Answer 6:

此警告也是有用的,以避免混淆声明返回指针不应该被修改的对象的功能时:

// "warning: type qualifiers ignored on function return type"
// as the pointer is copied. 
Foo* const bar();

// correct:
const Foo* bar();


Answer 7:

有之间的差异const的基本类型和结果,在那里它被忽略, const的一类类型的结果,它一般肆虐。

namespace i {
    auto f() -> int const { return 42; }
    void g( int&& ) {}
}

namespace s {
    struct S {};
    auto f() -> S const { return {}; }
    auto g( S&&  ) {}
}

auto main() -> int
{
    { using namespace i; g( f() ); }    // OK
    { using namespace s; g( f() ); }    // !The `const` prevents this.
}

这就是为什么编译器在第一种情况中警告说:这是一个特殊的情况下,可能不会做一个天真的可能会发生什么。

对于现代的编程那就恕我直言,是一个有关警告也不错const的类类型的结果,因为它禁止移动语义; 不管是什么小的一个优势设想了一个相当严峻的成本。



Answer 8:

斯科特迈尔斯指出,有相当充分的理由为什么会有人想返回const的值。 下面是一个例子:

int some_calculation(int a, int b) { int res = 0; /* ... */ return res; }

/* Test if the result of the calculation equals 40.*/
if (some_calculation(3,20) = 40)
{

}

你看我做错了什么? 此代码是完全正确的,应该编译。 问题是,编译器不明白,你打算比较,而不是分配40

随着const返回值上面的例子就不能编译。 嗯,至少如果编译器不放弃const关键字。



文章来源: Pedantic gcc warning: type qualifiers on function return type