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

前端之家收集整理的这篇文章主要介绍了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的实际实例化失败,所以不会使实际的声明形成错误,因此它会使用它并最终失败.

猜你在找的C&C++相关文章