问题
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