Pedantic gcc warning:类型限定符在函数返回类型 [英] Pedantic gcc warning: type qualifiers on function return type

查看:284
本文介绍了Pedantic gcc warning:类型限定符在函数返回类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我第一次编译我的C ++代码与GCC 4.3,(在编译成功后没有在4.1,4.0,3.4的警告与 -Wall -Wextra options)我突然有一堆错误的形式警告:类型限定符忽略函数返回类型

When I compiled my C++ code with GCC 4.3 for the first time, (after having compiled it successfully with no warnings on 4.1, 4.0, 3.4 with the -Wall -Wextra options) I suddenly got a bunch of errors of the form 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 是多余的,但我有麻烦理解为什么有必要生成一个警告。还有其他地方我应该离开const?

Can someone tell me what I am doing wrong that violates the C++ standard? I suppose that when returning by value, the leading const is superfluous, but I'm having trouble understanding why it's necessary to generate a warning with it. Are there other places where I should leave off the const?

推荐答案

它没有违反标准。这就是为什么他们会 而不是错误

It doesn't violate the standard. That's why they're warnings and not errors.

确实你是对的 - c $ c> const 是多余的。编译器警告你,因为你添加了代码,在其他情况下可能意味着什么,但在这种情况下没有什么,它想确保你不会失望以后当你的返回值是可以修改的。

And indeed you're right — the leading const is superfluous. The compiler warns you because you've added code that in other circumstances might mean something, but in this circumstance means nothing, and it wants to make sure you won't be disappointed later when your return values turn out to be modifiable after all.

这篇关于Pedantic gcc warning:类型限定符在函数返回类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆