C++ Is “const void” As Return Value More Const-Correct Than “void”? [duplicate]

无人久伴 提交于 2020-01-02 22:25:54

问题


I have become somewhat of a const-correctness fanatic when it comes to programming. I've got const's everywhere (where correct of course). Now I've even started const'ing my void return types.

You can't create a void object and therefore you can't assign a value to a void object even if it's const or not, which means the "const" becomes redundant.

So am I const'ing my void return types for nothing?

I hope this isn't too philosophical for Stack Overflow.

TL;DR:

const void Foo( void );

vs

void Foo( void );

Is there any difference?


回答1:


No, const void is completely meaningless. I'm surprised your compiler doesn't give you a warning, actually. Clang, for instance, told me:

example.cpp:1:1: warning: 'const' type qualifier on return type has no effect
      [-Wignored-qualifiers]
const void Foo( void );
^~~~~~
1 warning generated.


来源:https://stackoverflow.com/questions/28706996/c-is-const-void-as-return-value-more-const-correct-than-void

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!