C++ 模板参数推断失败,是否使用typedef?

C++ 模板参数推断失败,是否使用typedef?,c++,class,templates,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 typen

考虑到以下几类:

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类{
公众:
// ...
};
模板
结构别名{typedef A intA;};
B类{
公众:
// ...
模板B&运算符=(const typename别名::intA&_arg){};
};
当我尝试将类
A
的对象分配给类
B
的对象时,我得到以下编译错误:

模板参数推断/替换失败:无法推断模板参数“\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;
}
模板
A类{
公众:
// ...
};
模板
使用别名=A;
B类{
公众:
// ...
模板B&运算符=(常量别名&){return*this;};
};
void f()
{
B B;
A A;
b=a;
}

使用
模板化
可能会解决此问题

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;
}
模板
A类{
公众:
// ...
};
模板
使用别名=A;
B类{
公众:
// ...
模板B&运算符=(常量别名&){return*this;};
};
void f()
{
B B;
A A;
b=a;
}

我得到一个不同的错误(使用g++5.4):
需要在“alias::intA”之前加上“typename”,因为“alias”是一个从属范围
确实,以下内容为我编写:

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类{
公众:
// ...
};
模板
结构别名{typedef A intA;};
B类{
公众:
// ...
模板B&运算符=(const typename别名::intA&_arg){};
};

我认为原因是,
alias::intA
不是一个实际的类型,而是一个模板化的类型名。

我得到了一个不同的错误(使用g++5.4):
需要在“alias::intA”之前加上“typename”,因为“alias”是一个从属范围
确实,以下内容为我编写:

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类{
公众:
// ...
};
模板
结构别名{typedef A intA;};
B类{
公众:
// ...
模板B&运算符=(const typename别名::intA&_arg){};
};

我认为原因是,
alias::intA
不是一个实际的类型,而是一个模板化的typename。

问题是
intA
是一个从属名称。无法从从属名称推断模板。请参见示例:

您还缺少
typename
关键字

您可以显式指定运算符的类型:

template <typename T1, typename T2>
struct A{ };

template<typename _T>
struct alias { typedef A<int,_T> intA; };

struct B 
{
    template <typename T> B& operator=(const typename alias<T>::intA& _arg) { };
};

int main() 
{
    A<int,int> a;
    B b;
    b.operator=<int>(a);
    return 0;
}
模板
结构A{};
模板
结构别名{typedef A intA;};
结构B
{
模板B&运算符=(const typename别名::intA&_arg){};
};
int main()
{
A A;
B B;
b、 运算符=(a);
返回0;
}
或者,您可以使用模板化别名(带或不带函数)使用特定的非从属名称参数:

模板
结构A{};
模板
使用别名_int=A;
结构别名
{
模板
使用intA=A;
};
结构B
{
模板B&运算符=(常量别名{u int&{u arg){};
};
结构C
{
模板C&运算符=(常量别名::intA&_arg){};
};
int main()
{
A A;
B B;
C C;
b=a;
c=a;
返回0;
}

问题在于
intA
是一个从属名称。无法从从属名称推断模板。请参见示例:

您还缺少
typename
关键字

您可以显式指定运算符的类型:

template <typename T1, typename T2>
struct A{ };

template<typename _T>
struct alias { typedef A<int,_T> intA; };

struct B 
{
    template <typename T> B& operator=(const typename alias<T>::intA& _arg) { };
};

int main() 
{
    A<int,int> a;
    B b;
    b.operator=<int>(a);
    return 0;
}
模板
结构A{};
模板
结构别名{typedef A intA;};
结构B
{
模板B&运算符=(const typename别名::intA&_arg){};
};
int main()
{
A A;
B B;
b、 运算符=(a);
返回0;
}
或者,您可以使用模板化别名(带或不带函数)使用特定的非从属名称参数:

模板
结构A{};
模板
使用别名_int=A;
结构别名
{
模板
使用intA=A;
};
结构B
{
模板B&运算符=(常量别名{u int&{u arg){};
};
结构C
{
模板C&运算符=(常量别名::intA&_arg){};
};
int main()
{
A A;
B B;
C C;
b=a;
c=a;
返回0;
}

这不是问题所在,但以下划线开头,后跟大写字母(
\t
)的名称和包含两个连续下划线的名称保留供实现使用。不要在代码中使用它们。这不是问题所在,但以下划线开头,后跟大写字母(
\t
)的名称和包含两个连续下划线的名称保留供实现使用。不要在代码中使用它们。我忘记了代码示例中的
typename
关键字。如果相关的话,我正在尝试使用
-std=c++11
进行编译。使用-std=c++11为我编译。我忘记了代码示例中的
typename
关键字。如果相关的话,我正在尝试使用
-std=c++11
编译。使用-std=c++11为我编译。