为什么要使用 const 进行隐式转换? [英] Why const for implicit conversion?

查看:42
本文介绍了为什么要使用 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

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

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 第 6 行中找到了与用户定义类型相关的初始化程序部分

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

如果程序要求对 const 限定类型 T 的对象进行默认初始化,则 T 应是具有用户提供的默认构造函数的类类型.

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 的使用是否使转换按照 12.3 第 2 行明确"?
  3. const 是否会影响第 4 节中谈到的左值和右值?
  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?

推荐答案

这与转换是隐式并没有太大关系.此外,它与转换 并没有太大关系.这真的是关于 rvalueslvalues.

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 转换为类型 X 时,结果是一个 rvalue.在 C++ 中,转换的结果始终是右值(除非您转换为引用类型).在 C++ 中,将非常量引用附加到右值是非法的.

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.

例如,这段代码不会编译

For example, this code will not compile

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

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

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 可以编译,没有 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天全站免登陆