我试图弄清楚当存在可变参数模板构造函数和转换运算符时,C ++编译器如何解析隐式转换。这是一个简单的例子来说明:
当我写:
#include <iostream>
class A {
public:
A () {}
template<typename...tTypes> A (tTypes...pArgs) {
std::cout << __PRETTY_FUNCTION__ << std::endl;
}
};
class B {
public:
operator A () const {
std::cout << __PRETTY_FUNCTION__ << std::endl;
return A();
}
};
int main() {
B b;
A a = b;
}
运行时我得到这个输出:B::operator A() const
。所以它正在使用转换运算符(正如我所料)。 http://ideone.com/ZZ2uBz
但是当A
是模板时,结果会有所不同:
#include <iostream>
template<typename tType>
class A {
public:
A () {}
template<typename...tTypes> A (tTypes...pArgs) {
std::cout << __PRETTY_FUNCTION__ << std::endl;
}
};
class B {
public:
template<typename tType>
operator A<tType> () const {
std::cout << __PRETTY_FUNCTION__ << std::endl;
return A<tType>();
}
};
int main() {
B b;
A<float> a = b;
}
运行此程序时,我得到了这个输出:A<tType>::A(tTypes ...) [with tTypes = {B}; tType = float]
。所以它使用A
的可变参数构造函数而不是B
中的转换运算符。 http://ideone.com/u9Rxuh
有人可以向我解释为什么会有区别吗?转换运算符不应该优先于构造函数吗?
我知道我可以明确地调用转化运算符(A<float> a = b.operator A<float>();
),但这不是我想要的。
答案 0 :(得分:2)
在我看来,转换是模棱两可的,其他编译器按预期失败(或者至少,这是我的期望)。请参阅使用clang的结果示例。
要消除歧义,您可以使构造函数或转换运算符显式化 例如,使用:
template<typename tType>
class A {
public:
A () {}
template<typename...tTypes> explicit A (tTypes...pArgs) { /* ... */ }
};
或者这个:
class B {
public:
template<typename tType>
explicit operator A<tType> () const { return A<tType>(); }
};