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?
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With