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):
基本上这意味着即使对于(A)b的显式类型转换(即如果使用((A)b)),也可以防止它变为一个变量声明).它会使用static_cast的规则.现在来看看static_cast(5.2.9)的标准:
如果你做static_cast< A>(b),它基本上看到A(b)是否形成良好;它是.只是因为模板函数copy-constructor的实际实例化失败,所以不会使实际的声明形成错误,因此它会使用它并最终失败. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |