4
MyClass* const Func(const std::string& statename)

for this coverity is giving the error

Parse warning (PW.USELESS_TYPE_QUALIFIER_ON_RETURN_TYPE) type qualifier on return type is meaningless .

Do we really need to remove the const here.?

iammilind
  • 68,093
  • 33
  • 169
  • 336
Sudhakar
  • 93
  • 3
  • 9

3 Answers3

6

The warning is correct. The MyClass* const is not needed. It should be MyClass* simply. However, you don't need to remove it, but you should remove it.

The reason is, theoretically MyClass* const would prevent the return value of Func() from being edited. But that is anyway not editable even without const, as it's not an lvalue.See the demo here. So with/without const, the compiler will always generate error, for an attempt to modify the return value of Func().

iammilind
  • 68,093
  • 33
  • 169
  • 336
  • Thanks for the nice explanation. Does the same applies the following as well ? here also I ma getting the coverity warning.static char const * const GetInstanceName( InstanceId Id ) ; inline const GraphicClass *const GetLogo() const; – Sudhakar Jan 23 '12 at 11:35
  • static char const * const GetInstanceName( InstanceId Id ) ; inline const GraphicClass *const GetLogo() const; – Sudhakar Jan 23 '12 at 11:36
  • 1
    @Sudhakar, yes. Return type pointer is not an lvalue, so compiler will take care of making it unassignable. You don't need to make it `const`. – iammilind Jan 23 '12 at 14:05
  • Thank you. this info was helpful. – Sudhakar Jan 23 '12 at 14:52
  • One thing I'd add to the answer: Be careful that "MyClass* const" wasn't supposed to be "const MyClass*". If the const comes before the '*' then it IS meaningful, and maybe what the original programmer meant. – Joseph Garvin May 03 '12 at 20:40
5

The const in the return type (MyClass* const) is indeed completely pointless. At the same time, it does no harm other than making the code more verbose. I personally would remove it.

To understand why it's pointless, consider the following:

MyClass* p = Func(statement);

What difference did the const make?

In other words, returning T* const is conceptually no different to returning const int.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • any commnets on the following ? Does the same applies the following as well ? here also I am getting the coverity warning. static char const * const GetInstanceName( InstanceId Id ) ; inline const GraphicClass *const GetLogo() const; – Sudhakar Jan 23 '12 at 12:18
  • @Sudhakar: It does. In each of the declarations, one of the `consts` is pointless. – NPE Jan 23 '12 at 12:19
  • Thank you. this info was helpful. – Sudhakar Jan 23 '12 at 14:51
3

You don't need to remove the const to get working code, but the code would most certainly be better without the pointless const. That's what the "warning" word in front of the "error" says, too.

thiton
  • 35,651
  • 4
  • 70
  • 100
  • I understand that. Is the const here really not needed ? I think that it returns a unmodifiable pointer to a class. (i.e) we can not change the pointer. but we can change the contents – Sudhakar Jan 23 '12 at 10:53
  • And what do you do with that pointer that's returned? You assign it to some variable. It's the type of _that_ variable that matters. – Asya Kamsky Jan 24 '12 at 14:35