c++ - 使用 typedef 模板参数推导失败?

标签 c++ class templates typedef

考虑以下几个类:

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/

相关文章:

c++ - 开罗充满透明度

c++ - 为什么(成员)函数指针在 Visual C++ 中表现得如此怪异?

c++ - 将类对象初始化为引用内存

c++ - 相同模板参数的专门化

c++ - Eclipse CDT 无法解析模板类的 std::vector

c++ - 使用别名和 C++ 集成在 .qml 中实现 .ui 文件的逻辑

c++11:clang 在我的模板定义中拒绝 numeric_limits<>,而 gcc 接受它 - 这是正确的吗?

c++ - 使用单独编译时返回 typedef 类型

c# 类对象 - 存储/检索多值列表的最佳方法是什么?

c++ - friend 方法的参数列表中的不同模板特化