考虑以下几个类:
template <typename T1, typename T2>
class A{
public:
// ...
};
template<typename _T>
struct alias { typedef A<int,_T> intA; };
class B{
public:
// ...
template <typename _T> B& operator=(const typename alias<_T>::intA& _arg) { };
};
当我尝试分配 A<int,int>
类的对象时到类 B
的对象,我得到以下编译错误:
template argument deduction/substitution failed: couldn't deduce template parameter ‘_T’
有没有其他方法可以使用 typedef 作为 B::operator=()
的输入参数? ??
最佳答案
模板化使用
可能会解决问题
template <typename T1, typename T2>
class A{
public:
// ...
};
template<typename _T>
using alias = A<int,_T>;
class B{
public:
// ...
template <typename _T> B& operator=(const alias<_T>& ) { return *this; };
};
void f()
{
B b;
A<int, int> a;
b = a;
}
关于c++ - 使用 typedef 模板参数推导失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53242504/