gpt4 book ai didi

c++ - 转换运算符转换为仅模板参数不同的类?

转载 作者:太空宇宙 更新时间:2023-11-04 12:04:41 25 4
gpt4 key购买 nike

我希望能够上课 A<double>A<float> .这是我尝试过的:

#include<iostream>

template<class T1>
class A {
template<class T0>
operator A() {
std::cout << __PRETTY_FUNCTION__ << "\n";
}
};

template<class T0,class T1>
void bar( const A<T0>& a, const A<T1>& b )
{
reinterpret_cast< const A<T0> >(b);
}


int main()
{
A<float> a_f;
A<double> a_d;
bar(a_f,a_d);
}

编译器吐出:error: invalid cast from type ‘const A<double>’ to type ‘const A<float>’ .

是否可以将转换运算符转换为仅模板参数不同的类?如果是:如何?

最佳答案

template<class T0> operator A<T0> ...

如果没有模板参数,它会转换为 A<T1> ,即无操作。

关于c++ - 转换运算符转换为仅模板参数不同的类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12677081/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com