简体   繁体   English

我应该在C ++ 11中声明转换运算符吗?

[英]Should I declare conversion operators explicit in C++11?

In C++11,it is recommended: 在C ++ 11中,建议:

  1. to explicitly define our own copy/move constructors, so that the compiler does not do it itself (according to [1] ). 明确定义我们自己的复制/移动构造函数,以便编译器本身不执行复制/移动构造函数(根据[1] )。
  2. to explicitly declare one-argument constructors as explicit, to avoid implicit conversions (according to [2] ). 将一元构造函数显式声明为显式,以避免隐式转换(根据[2] )。

Should one, in this train of thought, declare conversion operators explicit to prevent the compiler from using them to perform implicit conversions? 在这种思路下,是否应该explicit声明转换运算符,以防止编译器使用它们执行隐式转换?

The answer is "no, you should not". 答案是“不,你不应该”。 Explicit conversion operators were added to the language to deal with a specific issue described below. 显式转换运算符已添加到该语言中,以处理下面描述的特定问题。 When you are not dealing with that specific issue, you should either add an explicit copy constructor to the destination class, or write a named function instead of the conversion operator (as in c_str() of std::string ). 当您不处理该特定问题时,应该将一个显式副本构造函数添加到目标类,或者编写一个命名函数而不是转换运算符(如std::string c_str() )。

Let's step back and consider conversion in general. 让我们退后一步,考虑一下转换。 When you need to write a conversion code to go from A to B you have two options - you can define a one-argument constructor, like this 当您需要编写一个从AB的转换代码时,有两个选择-您可以定义一个单参数构造函数,如下所示

struct B {
    B(const A& a);
};

or you can define a conversion operator, like this: 或者您可以定义一个转换运算符,如下所示:

struct A {
    operator B() const;
};

The first approach has allowed the explicit/implicit control prior to C++11. 第一种方法允许在C ++ 11之前进行显式/隐式控制。 However, it was not available in two situations: 但是,它在两种情况下不可用:

  • You own type A but you do not own type B , or 您拥有A型但没有B型,或者
  • You own type A , but type B is primitive 您拥有类型A ,但类型B原始类型

According to the draft paper for the explicit conversion operators of C++11, it is these two cases that the committee was addressing when they argued for adding the explicit conversion operator to the language. 根据针对C ++ 11的显式转换运算符的草稿草案 ,委员会在争论将这两种显式转换运算符添加到语言时正是在处理这两种情况。 All other situations were already covered by (1) implicit and implicit copy constructors, (2) implicit conversion operators, and (3) named member functions. (1)隐式和隐式副本构造函数,(2)隐式转换运算符和(3)命名成员函数已经涵盖了所有其他情况。

In C++11,it is recommended: 在C ++ 11中,建议:

  1. to explicitly define our own copy/move constructors, so that the compiler does not do it itself. 明确定义我们自己的复制/移动构造函数,以便编译器本身不执行此操作。

Whoever came up with that recommendation, it is wrong. 谁提出这个建议,那都是错误的。 Whenever the default implementation fits your needs, use it. 只要默认实现适合您的需求,就使用它。 You'll probably not going to get it better yourself. 您可能自己不会更好。

  1. to explicitly declare one-argument constructors as explicit, to avoid implicit conversions. 将一元构造函数显式声明为显式,以避免隐式转换。

The C++ Core Guidelines say "By default, declare single-argument constructors explicit ". C ++核心准则说“默认情况下, explicit声明单参数构造explicit ”。 You might prefer to have implicit construction in some cases (eg std::string has from const char* ). 在某些情况下,您可能更喜欢隐式构造(例如std::string来自const char* )。 Leave out the explicit declaration in those cases. 在这些情况下,请忽略explicit声明。

Should one, in this train of thought, declare conversion operators explicit to prevent the compiler from using them to perform implicit conversions? 在这种思路下,是否应该明确声明转换运算符,以防止编译器使用它们执行隐式转换?

There is no really good point in making them explicit. 明确地表达它们没有什么好处。 That would mean, that the conversion can only be used with a cast. 这意味着该转换只能与强制转换一起使用。 Casts are harder to read than getter function calls. 强制转换比getter函数调用更难阅读。 Write conversion operators, when you want implicit conversion, write getters when you want explicit conversion. 写转换运算符,当您要隐式转换时,写getters当您要显式转换时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM