微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c – 更喜欢转换运算符转换构造函数

我有以下代码片段:
class A
{
public:
  A() : x_(0),y_(0) {}

  A(int x,int y) : x_(x),y_(y) {}

  template<class T>
  A(const T &rhs) : x_(rhs.x_),y_(rhs.y_)
  {
  }

  int x_,y_;
};

class B
{
public:
  B() {}

  operator A() const { return A(c[0],c[1]); }
  int c[2];
};

void f()
{
  B b;
  (A)b; // << here the error appears,compiler tries to use 
        //         template<class T> A(const T &rhs)
}

为什么编译器使用A的构造函数?如何使用B的转换操作符到A?

我使用MSVS2010编译器.它给我这些错误

main.cpp(9): error C2039: 'x_' : is not a member of 'B'
          main.cpp(17) : see declaration of 'B'
          main.cpp(28) : see reference to function template instantiation 'A::A<B>(const T &)' being compiled
          with
          [
              T=B
          ]
main.cpp(9): error C2039: 'y_' : is not a member of 'B'
          main.cpp(17) : see declaration of 'B'

UPD:
好吧,隐含的转换,纳瓦兹说真的有效.让我们让它变得更加复杂,怎样使下面的代码工作?

void f()
{
  std::vector<B> b_vector(4);
  std::vector<A> a_vector( b_vector.begin(),b_vector.end() );
}

UPD:A是第三方lib中的类,我不能编辑代码,所以我不能删除A的转换构造函数.

UPD:我发现的最简单的解决方案是定义B的转换构造函数的专业化.它可以在第三方lib之外完成:

template<> A::A( const B &rhs ) : x_(rhs.c[0]),y_(rhs.c[1]) {}

解决方法

原因不仅仅是因为它认为(A)b与A(b)相同.该标准说明了关于显式类型转换(5.4):

The conversions performed by

  • a const_cast (5.2.11),

  • a static_cast (5.2.9),

  • a static_cast followed by a const_cast,

  • a reinterpret_cast (5.2.10),or

  • a reinterpret_cast followed by a const_cast,

can be performed using the cast
notation of explicit type conversion.
The same semantic restrictions and
behaviors apply.

基本上这意味着即使对于(A)b的显式类型转换(即如果使用((A)b)),也可以防止它变为一个变量声明).它会使用static_cast的规则.现在来看看static_cast(5.2.9)的标准:

An expression e can be explicitly
converted to a type T using a
static_cast of the form
static_cast(e) if the declaration
“T t(e);” is well-formed,for some
invented temporary variable t (8.5).
The effect of such an explicit
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 a
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.

如果你做static_cast< A>(b),它基本上看到A(b)是否形成良好;它是.只是因为模板函数copy-constructor的实际实例化失败,所以不会使实际的声明形成错误,因此它会使用它并最终失败.

原文地址:https://www.jb51.cc/c/111572.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐