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

c – 更喜欢转换运算符转换构造函数,第1张

概述我有以下代码片段: 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: @H_404_0@我有以下代码片段:
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 – 更喜欢转换运算符转换构造函数所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1255443.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存