Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

warning: type qualifiers ignored on function return type [-Wignored-qualifiers]

I'm trying to compile the below code using GCC compiler

class Class
{
public:

uInt16    temp;
const uInt32 function() const;

}

inline const uInt32 class::function() const
{
   return temp;
}

And I'm getting the following compiler warning

warning: type qualifiers ignored on function return type [-Wignored-qualifiers]

Any ideas how I can fix this warning?

like image 759
Nataraja Avatar asked Oct 23 '25 04:10

Nataraja


2 Answers

Use simply:

uInt32 function() const;

Returning const primitive type is useless, as you cannot do c.function()++ even without const.

Returning const Object was used to mimic primitives and forbid code similar to above, but now (since C++11), we can forbid that cleanly, if required:

struct S
{
    S& operator ++() &;
    S& operator ++() && = delete;
};

S f(); // Sufficient, no need of const S f() which forbids move
like image 98
Jarod42 Avatar answered Oct 25 '25 20:10

Jarod42


The const type qualifier on the return type has no effect. Indeed, your function returns a copy of temp. It's the caller who will decide whether this value has to be const or not:

const auto val = Class{}.function(); // here, val is const
auto val = Class{}.function(); // here val is not const

The const type qualifier makes sense if, for example, you want to return a reference to a class member. Compare:

int f() { /* ... */ } // return int
const int& f() { /* ... */ } // return const reference to an int
like image 26
mfnx Avatar answered Oct 25 '25 19:10

mfnx