简体   繁体   English

为什么这个编译器警告只显示 int 而不是 string? “在 function 返回类型上忽略类型限定符”

[英]Why does this compiler warning only show for int but not for string? "type qualifiers ignored on function return type"

I am a bit confused about some warnings I get when compiling my C++11 code using mingw64.我对使用 mingw64 编译 C++11 代码时收到的一些警告有点困惑。 This is my MWE:这是我的 MWE:

class A{
    const string name;
    const int ID;

    public:
        A(string name_, int ID_) : name(name_), ID(ID_){
            // initialize non-const members
        }
        const string getName() const{return name;}
        const int getID() const{return ID;}
};

int main()
{   
    A aObj = A("Aname", 1);
    std::cout << "getName() = " << aObj.getName() << std::endl;
    std::cout << "getID() = " << to_string(aObj.getID()) << std::endl;
}

Code executes fine and does what it should, but I get this compiler warning:代码执行得很好并且做它应该做的,但是我得到这个编译器警告:

,,localtest.cpp:10:9: warning: type qualifiers ignored on function return type ,,localtest.cpp:10:9: 警告:在 function 返回类型上忽略类型限定符

[-Wignored-qualifiers] const int getID() const{return ID;} [-Wignored-qualifiers] const int getID() const{return ID;}

So the warning only shows for getID() but not for getName() , even though both have the same type qualifiers.所以警告只显示getID()而不是getName() ,即使两者具有相同的类型限定符。 Can somebody explain to me, why this warning seems only to show for string but not for int ?有人可以向我解释一下,为什么这个警告似乎只显示string而不是int I suppose it has something to do with int being a primitive data type - but what exactly?我想这与int作为原始数据类型有关 - 但究竟是什么?

std::string is a class that has member functions that can be constant. std::string是一个 class ,它的成员函数可以是常量。 If you have a constant object of the class you may apply only constant member functions.如果你有一个常数 object 的 class 你可以只应用常数成员函数。

As for fundamental types like for example int then the qualifier const does not make a sense for a return value because in any case you can not change the returned value.至于像int这样的基本类型,那么限定符 const 对返回值没有意义,因为在任何情况下您都无法更改返回值。

Here is a demonstrative program这是一个演示程序

#include <iostream>
#include <string>

template <typename T>
const T f( const T &t )
{
    return t;
}

int main() 
{
    std::cout << f( std::string( "Hello World!" ) ).length() <<  '\n';

//  Invalid assignment  
//  f( 10 ) = 20;
    
    return 0;
}

The program output is程序 output 是

12

As you can see you can apply constant member functions to the returned object of the type std::string (but you can not apply non-constant member functions).如您所见,您可以将常量成员函数应用于std::string类型的返回 object (但您不能应用非常量成员函数)。 And you can not change the returned value of the type int .而且您不能更改int类型的返回值。

Consider the following:考虑以下:

struct MyType {
  void foo() const;
  void bar();
};

MyType getMutable();
const MyType getConst();

int main() {
  getMutable().foo(); // fine
  getMutable().bar(); // fine
  getConst().foo(); // fine
  getConst().bar(); // Not allowed!
}

There just isn't anything equivalent for int . int没有任何等价物。 The set of operations you can do on a int RValue is the exact same as for a const int RValue.您可以对int RValue 执行的一组操作与const int RValue 完全相同。 That's why you are getting a redundancy warning.这就是您收到冗余警告的原因。

See [expr.type] :[expr.type]

If a prvalue initially has the type “cv T”, where T is a cv-unqualified non-class, non-array type, the type of the expression is adjusted to T prior to any further analysis.如果prvalue最初具有类型“cv T”,其中T是cv-unqualified non-class,non-array类型,则表达式的类型在任何进一步分析之前调整为T。

The essence is: you can have expressions of const class or array types, but not of const primitive types.本质是:你可以有const class 或数组类型的表达式,但不能有const原始类型的表达式。

Since int is not a class, it is enough for return type to be rvalue to prevent any and all modifications of the returned object.由于int不是 class,因此返回类型为rvalue就足以防止对返回的 object 进行任何和所有修改。 Thus,因此,

getInt(20) = 500;

would not be compilable code, and there are no members you could invoke on objects of int type.不会是可编译的代码,并且没有可以在int类型的对象上调用的成员。 This is why const-qualifying built-in types as return values make no sense, and compiler is warning you about that.这就是为什么 const 限定内置类型作为返回值没有意义,编译器会警告你。

But the situation is different for the classes.但不同班级的情况有所不同。

getString("string").clear();

Might be either valid or invalid code, based on whether getString returns non-const or const std::string object, thus compiler is not issuing a warning in the latter case.可能是有效或无效代码,取决于getString返回非 const 还是 const std::string object,因此编译器在后一种情况下不会发出警告。

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

相关问题 警告:在函数返回类型 [-Wignored-qualifiers] 上忽略类型限定符 - warning: type qualifiers ignored on function return type [-Wignored-qualifiers] 为什么仅在原语的情况下才在函数返回类型中忽略类型限定符? - Why type qualifiers ignored on function return type only in case of primitives? Pedantic gcc warning:在函数返回类型上输入限定符 - Pedantic gcc warning: type qualifiers on function return type 为什么在某些情况下 cv 限定符会从 function 返回类型中删除? - Why do cv-qualifiers get removed from function return type in some cases? 编译器警告:无法推断lambda返回类型 - Compiler warning: lambda return type cannot be deduced 为什么编译器不为返回类型为classname&的函数不返回该指针而抛出错误 - Why does'nt the compiler throw error for not returning this pointer for a function whose return type is classname& 为什么编译器在已经具有类资格的成员函数定义的返回类型上要求类限定符? - Why does the compiler require the class qualifier on the return type of an already class-qualified member function definition? 编译器对函数返回类型的验证 - Function return type validation by compiler 为什么编译器将此方法的返回类型报告为void? - Why does the compiler report the return type of this method as void? 为什么编译器需要尾随返回类型? - Why does the compiler want a trailing return-type?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM