C++ 我应该返回 bool 还是 const bool?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1443659/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Should I return bool or const bool?
提问by Ben Hymers
Which is better:
哪个更好:
bool MyClass::someQuery() const;
const bool MyClass::someQuery() const;
I've been using 'const bool' since I'm sure I remember hearing it's "what the ints do" (for e.g. comparison operators) but I can't find evidence of that anywhere, mostly due to it being difficult to Google and Intellisense not helping out any ;) Can anyone confirm that?
我一直在使用“const bool”,因为我确定我记得听到它是“整数做什么”(例如比较运算符),但我在任何地方都找不到证据,主要是因为它很难谷歌和Intellisense 没有帮助任何 ;) 任何人都可以确认吗?
To me returning const values (this isn't just about bools) makes more sense; it'll prevent temporaries being modified, which is almost always going to be a programmer mistake. I just want something to back that up so I can extol returning const values to my colleagues :)
对我来说,返回 const 值(这不仅仅是关于 bool 的)更有意义;它将防止临时文件被修改,这几乎总是程序员的错误。我只是想要一些东西来支持它,这样我就可以赞美将 const 值返回给我的同事:)
回答by sharptooth
This is the case when const
adds no value but inflates the code and makes the reader think more. What's the point of this const
? The caller can copy the value into some non-const variable and do whatever he wants with it anyway.
这就是const
不增加任何价值但膨胀代码并使读者思考更多的情况。这有什么意义const
?调用者可以将值复制到某个非常量变量中,并且无论如何都可以对它做任何他想做的事情。
回答by moonshadow
So you know it's right, you're just after the Voice of Authority?
Preventing accidental modification of temporaries is very valuable. In general, you should declare as many things as you possibly can const
, it protects you from a variety of accidents and gives the optimiser useful hints.
所以你知道这是对的,你只是在追求权威之声?防止临时修改的意外修改是非常有价值的。一般来说,你应该尽可能多地声明一些东西const
,它可以保护你免受各种事故的影响,并为优化器提供有用的提示。
D'you have a copy of Scott Meyers' "Effective C++" around? Point them at Item 3 (page 18 in the third edition) ;)
你身边有 Scott Meyers 的“Effective C++”吗?将它们指向第 3 项(第三版第 18 页);)
It gives the example of
它给出了例子
class Rational {...};
const Rational operator* (const Rational& lhs, const Rational& rhs );
if( (a * b) = c ) // declaring operator *'s return value const causes error to be caught by compiler
回答by sbi
Note that if((a*b) = c)
won't compile for built-in types anyway, so it is very relevant here whether we're talking built-in types (your question asks for bool
) or user-defined types.
请注意,if((a*b) = c)
无论如何都不会针对内置类型进行编译,因此无论我们是在谈论内置类型(您的问题要求bool
)还是用户定义类型,这里都非常相关。
For built-in types it makes no sense at all, so it shouldn't be used. And for user-defined types, I'm in jalf's camp: What if the caller wantsto modify the returned object?
对于内置类型,它根本没有意义,所以不应该使用它。对于用户定义的类型,我在 jalf 的阵营:如果调用者想要修改返回的对象怎么办?
I'm not convinced that if((a*b) = c)
is such a good argument for returning const user-defined types, since I can't remember the last time I've seen a compiler notwarn about this.
我不相信这if((a*b) = c)
是返回 const 用户定义类型的一个很好的论据,因为我不记得上次我看到编译器没有对此发出警告是什么时候。
回答by sellibitze
To be a little more specific, only "objects" can be const. The C++ standard's definition of "object" includes everything an lvalue refers to ("has a name") and class-type temporaries. A boolean return value is an rvalue of a non-class type which is why a standards-compliant compiler will just ignore "const" in this case. As others said already, it's useless in this context.
更具体地说,只有“对象”可以是常量。C++ 标准对“对象”的定义包括左值所指的所有内容(“具有名称”)和类类型临时变量。布尔返回值是非类类型的右值,这就是为什么在这种情况下符合标准的编译器将忽略“const”。正如其他人已经说过的那样,在这种情况下它是无用的。
回答by Naveen
When you returning a refernce to a member variable it makes sense to make it const. Here you are returning a copy, hence there is no need of const.
当您返回对成员变量的引用时,将其设置为 const 是有意义的。在这里您将返回一个副本,因此不需要 const。
回答by Tony the Pony
The const
modifier is only used for return types that are returned by reference(either as reference const SomeObject&
or via a pointer const SomeObject*
), so the caller won't be able to modify the object via the reference/pointer. Primitive types are returned by value, which means that the caller receives a copyof the the object, not the object itself.
所述const
改性剂仅用于返回的返回类型以引用(或者作为参考const SomeObject&
或通过指针const SomeObject*
),因此主叫方将不能够通过参考/指针修改对象。原始类型由 value返回,这意味着调用者接收对象的副本,而不是对象本身。
Therefore, const
is not really appropriate for returned value types. Since the copy is outside of the control of the called function, the called function should not dictate to the caller that it cannot be changed.
因此,const
对于返回值类型并不真正合适。由于副本不在被调用函数的控制范围内,因此被调用函数不应指示调用者它不能被更改。
回答by MSalters
It completely doesn't matter. Therefore, the consensus is to return just bool
.
完全没有关系。因此,共识是返回 just bool
。
The reason that it doesn't matter is that you can't call non-const member functions anyway; bool
is not a class or struct.
没关系的原因是你无论如何都不能调用非常量成员函数;bool
不是类或结构。
回答by jdehesa
This is an ancient post, but I think it's worth mentioning there is a potential corner case here since C++11. While, as stated by others, it will make no difference whether you use const bool
or bool
as return type in most cases, if you are using C++11 decltype
and associates, e.g. result_of
, you could declare a variable with the same type as the returning value of some function, and so the const
would actually have an effect in this case.
这是一篇古老的帖子,但我认为值得一提的是,自 C++11 以来,这里有一个潜在的极端情况。虽然,正如其他人所说,在大多数情况下,无论是使用const bool
还是bool
作为返回类型都没有区别,但如果您使用的是 C++11decltype
和关联,例如result_of
,您可以声明一个与返回值具有相同类型的变量一些函数,因此const
在这种情况下实际上会产生影响。
回答by user175491
As bool is going to be copied, it's the same, to put const or not. Plus you'll may have some compil problems.
由于 bool 将被复制,因此无论是否放置 const,都是一样的。另外,您可能会遇到一些编译问题。
回答by adatapost
SUMMARY:
The value of a return type that is declared const cannot be changed. This is especially usefull when giving a reference to a class's internals, but can also prevent rarer errors.
概括:
声明为 const 的返回类型的值不能更改。这在提供对类内部的引用时特别有用,但也可以防止更罕见的错误。