通过 const 值返回基本类型有意义吗?

发布于 2024-08-05 07:09:54 字数 397 浏览 1 评论 0原文

哪个更好:

bool MyClass::someQuery() const;

const bool MyClass::someQuery() const;

我一直在使用 const bool ,因为我确信我记得听说过它是“int 的作用”(例如比较运算符),但我可以在任何地方都找不到这方面的证据,主要是因为 Google 和 IntelliSense 很难提供任何帮助;)任何人都可以证实这一点吗?

对我来说,返回 const 值(这不仅仅是 bool )更有意义;它将防止临时变量被修改,这几乎总是程序员的错误。我只是想要一些东西来支持这一点,这样我就可以向我的同事赞扬返回 const 值:)

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?

To me returning const values (this isn't just about bool) 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 :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

紧拥背影 2024-08-12 07:09:54

const 没有增加任何值,而是使代码膨胀并让读者思考更多时,就是这种情况。这个const有什么意义?调用者可以将值复制到某个非常量变量中,然后用它做任何他想做的事情。

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.

街角迷惘 2024-08-12 07:09:54

所以你知道这是对的,你只是在寻找权威之声?
防止临时变量的意外修改非常有价值。一般来说,您应该声明尽可能多的东西 const,它可以保护您免受各种意外的影响,并为优化器提供有用的提示。

你有 Scott Meyers 的《Effective C++》吗?将他们指向第 3 项(第三版第 18 页);)

它给出了以下示例

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

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.

D'you have a copy of Scott Meyers' "Effective C++" around? Point them at Item 3 (page 18 in the third edition) ;)

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
念﹏祤嫣 2024-08-12 07:09:54

请注意, if((a*b) = c) 不会编译无论如何,内置类型,因此无论我们谈论内置类型(您的问题要求 bool)还是用户定义类型,这里都非常相关。

对于内置类型来说它根本没有意义,所以不应该使用它。对于用户定义的类型,我站在 jalf 的阵营:如果调用者想要修改返回的对象怎么办?

我不相信 if((a*b) = c) 是返回 const 用户定义类型的一个很好的论据,因为我不记得上次看到一个编译器不会对此发出警告。

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.

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 wants to modify the returned object?

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 not warn about this.

爱殇璃 2024-08-12 07:09:54

更具体地说,只有“对象”可以是 const。 C++ 标准对“对象”的定义包括左值引用的所有内容(“有名称”)和类类型临时变量。布尔返回值是非类类型的右值,这就是为什么符合标准的编译器在这种情况下将忽略“const”。正如其他人已经说过的,在这种情况下它是没有用的。

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.

一江春梦 2024-08-12 07:09:54

当您返回对成员变量的引用时,将其设置为 const 是有意义的。这里你返回一个副本,因此不需要 const。

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.

画离情绘悲伤 2024-08-12 07:09:54

const 修饰符仅用于通过引用返回的返回类型(作为引用 const SomeObject& 或通过指针 const SomeObject*),因此调用者将无法通过引用/指针修改对象。原始类型按值返回,这意味着调用者收到对象的副本,而不是对象本身。

因此,const 并不真正适合返回值类型。由于副本不在被调用函数的控制范围内,因此被调用函数不应向调用者指示它不能更改。

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 copy of the the object, not the object itself.

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.

⊕婉儿 2024-08-12 07:09:54

这是一篇古老的文章,但我认为值得一提的是,自 C++11 以来,这里存在一个潜在的极端情况。然而,正如其他人所述,如果您使用的是 C++11 ,在大多数情况下,使用 const boolbool 作为返回类型都没有什么区别>decltype 和关联,例如 result_of,您可以声明一个与某个函数的返回值具有相同类型的变量,因此 const 在这种情况下实际上会产生效果。

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.

原谅我要高飞 2024-08-12 07:09:54

完全没关系。因此,共识是仅返回 bool

没关系的原因是无论如何你都不能调用非常量成员函数; bool 不是类或结构。

It completely doesn't matter. Therefore, the consensus is to return 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.

月下客 2024-08-12 07:09:54

因为 bool 会被复制,所以无论是否放置 const 都是一样的。另外你可能会遇到一些编译问题。

As bool is going to be copied, it's the same, to put const or not. Plus you'll may have some compil problems.

红衣飘飘貌似仙 2024-08-12 07:09:54

常量返回类型

摘要:

返回类型的值是
声明的 const 不能更改。这
当给予
引用类的内部,但是
还可以防止罕见的错误。

const return type

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.

浪荡不羁 2024-08-12 07:09:54
const bool func();
bool f = func();

0 个错误,0 个警告。除了不必要的代码膨胀之外,您还完成了什么?

const bool func();
bool f = func();

0 errors, 0 warnings. What have you accomplished other than unnecessary code inflation?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文