为什么const用于隐式转换? [英] Why const for implicit conversion?

查看:119
本文介绍了为什么const用于隐式转换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在广泛阅读 ISO / IEC 14882,编程语言 - C ++ 我仍然不确定为什么 const 需要隐式转换为具有单个参数构造函数的用户定义类型,如下所示

After extensive reading of ISO/IEC 14882, Programming language – C++ I'm still unsure why const is needed for implicit conversion to a user-defined type with a single argument constructor like the following

#include <iostream>

class X {
public:
   X( int value ) {
      printf("constructor initialized with %i",value);
   }
}

void implicit_conversion_func( const X& value ) {
   //produces "constructor initialized with 99"
}

int main (int argc, char * const argv[]) {
   implicit_conversion_func(99);
}




从第4节开始第3行



Starting with section 4 line 3


当且仅当声明T t = e时,表达式e可以隐式转换为类型T;是良好形成的,对于一些发明的临时变量t(8.5)。某些语言结构要求将表达式转换为布尔值。出现在这样的上下文中的表达式e被称为上下文地转换为bool,并且当且仅当声明bool t(e)时才是良好形式的;是良好形成的,对于一些发明的临时变量t(8.5)。隐式转换的效果与执行声明和初始化相同,然后使用临时变量作为转换结果。如果T是一个左值引用类型(8.3.2),结果是一个左值,否则为右值。

An expression e can be implicitly converted to a type T if and only if the declaration T t=e; is well-formed, for some invented temporary variable t (8.5). Certain language constructs require that an expression be converted to a Boolean value. An expression e appearing in such a context is said to be contextually converted to bool and is well-formed if and only if the declaration bool t(e); is well-formed, for some invented temporary variable t (8.5). The effect of either implicit conversion is the same as performing the declaration and initialization and then using the temporary variable as the result of the conversion. The result is an lvalue if T is an lvalue reference type (8.3.2), and an rvalue otherwise. The expression e is used as an lvalue if and only if the initialization uses it as an lvalue.

之后,我找到了一个关于与第8.5行中的用户定义类型相关的初始化器

Following that I found the section on initializers related to user-defined types in 8.5 line 6


如果程序调用const限定类型的对象的默认初始化

If a program calls for the default initialization of an object of a const-qualified type T, T shall be a class type with a user-provided default constructor.

最后,我最后在12.3行第2行关于用户提供的默认构造函数。定义的转换,其状态

Finally I ended up at 12.3 line 2 about user-defined conversions which states


用户定义的转换仅在它们是明确的(10.2,12.3.2)时应用。

User-defined conversions are applied only where they are unambiguous (10.2, 12.3.2).

不用说,10.2和12.3.2没有回答我的问题。



Needless to say, 10.2 and 12.3.2 didn't answer my question.


  1. 某人可以了解什么效果 const 对隐式转换有何影响?

  2. 使用 const 使转换明确 ?

  1. Can someone shed some light on what effect const has on implicit conversions?
  2. Does the use of const make the conversion "unambiguous" per 12.3 line 2?
  3. Does const somehow affect lvalue vs. rvalue talked about in section 4?

const 会不会影响第4节中讨论的左值与右值? >

推荐答案

这不是真正的转换隐含。此外,它实际上与转换没有太大关系。

It doesn't really have much to do with the conversion being implicit. Moreover, it doesn't really have much to do with conversions. It is really about rvalues vs. lvalues.

当您转换 99 时, code>键入 X ,结果是 rvalue 。在C ++中,转换结果始终为右值(除非转换为引用类型)。在C ++中将非const引用附加到右值是非法的。

When you convert 99 to type X, the result is an rvalue. In C++ results of conversions are always rvalues (unless you convert to reference type). It is illegal in C++ to attach non-const references to rvalues.

例如,此代码不会编译

X& r = X(99); // ERROR

因为它试图将非const引用附加到右值。另一方面,这段代码很好

because it attempts to attach a non-const reference to an rvalue. On the other hand, this code is fine

const X& cr = X(99); // OK

因为将一个const引用附加到右值是完全正确的。

because it is perfectly OK to attach a const reference to an rvalue.

同样的事情也发生在你的代码中。事实上,它涉及隐式转换是一个旁边的点。你可以用显式
替换隐式转换。

The same thing happens in your code as well. The fact that it involves an implicit conversion is kinda beside the point. You can replace implicit conversion with an explicit one

implicit_conversion_func(X(99));

并以相同的情况: const it编译,没有 const 它不。

and end up with the same situation: with const it compiles, without const it doesn't.

同样,转换(显式或隐式)的唯一作用是它帮助我们产生一个右值。一般来说,您可以用其他方式产生右值,并执行相同的问题。

Again, the only role the conversion (explicit or implicit) plays here is that it helps us to produce an rvalue. In general, you can produce an rvalue in some other way and run into the same issue

int &ir = 3 + 2; // ERROR
const int &cir = 3 + 2; // OK

这篇关于为什么const用于隐式转换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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