简体   繁体   English

Pedantic gcc warning:在函数返回类型上输入限定符

[英]Pedantic gcc warning: type qualifiers on function return type

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 . 当我第一次使用GCC 4.3编译我的C ++代码时,(在成功编译它并且在-Wall -Wextra上没有使用-Wall -Wextra选项的警告之后)我突然遇到了一堆表单warning: type qualifiers ignored on function return type的错误warning: type qualifiers ignored on function return type

Consider temp.cpp : 考虑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;
};

Running g++ temp.cpp -Wextra -c -o blah.o : 运行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

Can someone tell me what I am doing wrong that violates the C++ standard? 有人能告诉我我做错了什么违反了C ++标准吗? 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. 我认为当按值返回时,前导const是多余的,但我无法理解为什么需要用它生成警告。 Are there other places where I should leave off the const? 还有其他地方我应该离开const吗?

It doesn't violate the standard. 它没有违反标准。 That's why they're warnings and not errors . 这就是为什么他们是警告而不是错误

And indeed you're right — the leading const is superfluous. 确实你是对的 - 领先的const是多余的。 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. 编译器警告你,因为你添加的代码在其他情况下可能意味着什么,但在这种情况下没有任何意义,并且它希望确保在你的返回值结果可以修改之后你不会失望。

I encountered this warning when compiling some code that uses Boost.ProgramOptions. 编译一些使用Boost.ProgramOptions的代码时遇到此警告。 I use -Werror so the warning was killing my build, but because the source of the warning was in the depths of Boost I couldn't get rid of it by modifying my code. 我使用-Werror所以警告是杀死我的构建,但因为警告的来源是在Boost的深处,我无法通过修改我的代码来摆脱它。

After much digging I found the compiler option that disables the warning: 经过多次挖掘后,我发现了禁用警告的编译器选项:

-Wno-ignored-qualifiers

Hope this helps. 希望这可以帮助。

Returning a constant value only makes sense when you return a reference or a pointer(in this case pointer to constant and not a constant pointer) because the caller is able to modify the referenced (pointed to) value. 返回一个常量值只有在返回引用或指针时才有意义(在这种情况下指针指向常量而不是常量指针),因为调用者能够修改引用的(指向的)值。

Another comment on the code not related to your question: I think it's better to use a setter instead of 关于与您的问题无关的代码的另一个评论:我认为最好使用setter而不是

int& getNonconstReference() {
    return _myInt;
}

Which will should be: 应该是:

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

Moreover, it's useless to return a const reference to an int. 而且,将一个const引用返回给int是没用的。 It does make sense for a bigger object whose copy or move is more expensive. 对于复制或移动更昂贵的更大对象,它确实有意义。

Having this 有这个

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

and that 然后

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

the example 这个例子

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

compiles without a warning. 在没有警告的情况下编译。 Of course, this is rare. 当然,这种情况很少见。 But isn't const correctness about making it hard for people to do things wrong? 但是,让人们做错事情并不是一般的正确性吗? And with the expectation that people try things, that are wrong, the return type should be declared const. 并且期望人们尝试事情,这是错误的,返回类型应该被声明为const。 And: g++ warns about ignoring the classifier, but does not ignore it. 并且:g ++警告忽略分类器,但不要忽略它。 I think, the warning is about users that take the copy and ignore the const classifiers on their copy. 我认为,警告是关于获取副本并忽略副本上的const分类器的用户。 But that should not be a warning, because this is absolutely correct behavior. 但这不应该是一个警告,因为这是绝对正确的行为。 And it makes sense to do this. 这样做是有道理的。

This warning is also useful to avoid confusion when declaring functions returning pointers to objects which should not be modified: 在声明返回指向不应修改的对象的指针的函数时,此警告也有助于避免混淆:

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

// correct:
const Foo* bar();

Shouldn't -pedantic only allow strict adherence to the ISO standard? 不应该 - 只允许严格遵守ISO标准吗? Depending on -std= of course... 取决于-std =当然......

There is a difference between const on a basic type result, where it's ignored, and const on a class type result, where it generally wreaks havoc. 有之间的差异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.
}

This is why the compiler warns in the first case: it's a special case, that may not do what one naïvely could expect. 这就是编译器在第一种情况下发出警告的原因:它是一种特殊情况,可能无法实现人们所能想到的。

For modern programming it would IMHO be nice also with a warning about const on class type result, since it prohibits move semantics; 对于现代编程,恕我直言也很好,同时警告关于类类型结果的const ,因为它禁止移动语义; a rather severe cost for whatever little advantage one envisioned. 对于任何想象的小优势而言,这是相当严重的成本。

Scott Meyers pointed out that there's pretty good reason why someone would want to return const values. Scott Meyers指出,为什么有人想要返回const值是有充分理由的。 Here's an example: 这是一个例子:

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)
{

}

Do you see what I did wrong? 你看到我做错了吗? This code is absolutely correct and should compile. 这段代码绝对正确,应该编译。 The problem is that the compiler didn't understand that you intended to compare instead of assign the value 40 . 问题是编译器不理解您打算比较而不是分配40

With a const return value the above example won't compile. 使用const返回值,上面的示例将无法编译。 Well, at least if the compiler doesn't discard the const keyword. 好吧,至少如果编译器没有丢弃const关键字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 警告:在函数返回类型 [-Wignored-qualifiers] 上忽略类型限定符 - warning: type qualifiers ignored on function return type [-Wignored-qualifiers] 为什么这个编译器警告只显示 int 而不是 string? “在 function 返回类型上忽略类型限定符” - Why does this compiler warning only show for int but not for string? "type qualifiers ignored on function return type" 为什么仅在原语的情况下才在函数返回类型中忽略类型限定符? - Why type qualifiers ignored on function return type only in case of primitives? 返回类型为类时,带尾随返回类型的GCC属性警告 - GCC attribute warning with trailing return type when return type is a class C ++返回类型限定符天堂 - C++ return type qualifiers heaven Object具有与成员函数不兼容的类型限定符 - Object has type qualifiers that are not compatible with the member function 带有限定符的 function 类型类型定义的用例 - Use cases for function type typedefs with qualifiers 从函数类型中剥离所有限定符 - Stripping all qualifiers from a function type 该对象具有与成员函数不兼容的类型限定符 - the object has type qualifiers that are not compatible with the member function 为什么在某些情况下 cv 限定符会从 function 返回类型中删除? - Why do cv-qualifiers get removed from function return type in some cases?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM