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> ...
아마도 템플릿 복사 생성자입니까? 아마도 – chris
. 내가 캐스팅을 원하는 이유는 캐스팅이 no-op 여야한다는 것입니다. 캐스팅이 noop 일 때 같은 유형으로 캐스팅 되어야만합니다. – ritter
"캐스팅이 무의미해야합니다."는 말이 맞지 않습니다. 일반 복사 생성자를 선택하라는 의미입니까? 생성자 템플릿 접근법을 취하면 그렇게 될 것입니다. –