C ++ is“const void”作为返回值更多的Const-Correct比“void”? [英] C++ Is "const void" As Return Value More Const-Correct Than "void"?

查看:243
本文介绍了C ++ is“const void”作为返回值更多的Const-Correct比“void”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我谈到编程时,我已经成为一个const正确的狂热者。我有const无处不在(当然正确)。现在我甚至开始const我的void返回类型。



你不能创建一个void对象,因此你不能给一个void对象分配一个值,即使它是const或者不是,这意味着const成为多余的。



我希望这个isn'

 对于Stack Overflow, const void Foo(void); 

vs

  void Foo(void); 

有什么区别吗?

解决方案

不, const void 是完全没有意义的。我很惊讶你的编译器不给你警告,实际上。例如,Clang告诉我:

  example.cpp:1:1:warning:'const'没有效果
[-Wignored-qualifiers]
const void Foo(void);
^ ~~~~~
1生成警告。


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?

解决方案

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.

这篇关于C ++ is“const void”作为返回值更多的Const-Correct比“void”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆