无法从模板库导入 typedef class
Unable to import of typedefs from template base class
这个问题不是重复的,而是跟进 Propagating 'typedef' from based to derived class for 'template'
作为继承 typedef 的解决方案,建议使用 using
将它们导入派生的 class,而简单的 using typename Base::typedefed_type
应该就足够了。
以下代码大部分摘自Roman Kruglov's answer:
#include <vector>
template<typename T>
class A
{
public:
typedef std::vector<T> Vec_t;
};
template<typename T>
class B : public A<T>
{
public:
using typename A::Vec_t;
// .........
private:
Vec_t v;
};
int main()
{
B<int> bb;
}
但是编译失败,因为编译器非常想要 A
.
的模板参数
英特尔编译器错误信息:
1>C:\Work\EDPS\test_eigen\test_eigen.cpp(27): error : argument list for class template "A" is missing
1> using typename A::Vec_t;
1> ^
1> detected during instantiation of class "B<T> [with T=int]" at line 34
1>
1>C:\Work\EDPS\test_eigen\test_eigen.cpp(31): error : identifier "Vec_t" is undefined
1> Vec_t v;
1> ^
1> detected during instantiation of class "B<T> [with T=int]" at line 34
1>
MVC 错误信息:
c:\work\edps\test_eigen\test_eigen.cpp(27): error C2955: 'A': use of class template requires template argument list
1>c:\work\edps\test_eigen\test_eigen.cpp(17): note: see declaration of 'A'
1>c:\work\edps\test_eigen\test_eigen.cpp(32): note: see reference to class template instantiation 'B<T>' being compiled
1>c:\work\edps\test_eigen\test_eigen.cpp(27): error C3210: 'A': a member using-declaration can only be applied to a base class member
1>c:\work\edps\test_eigen\test_eigen.cpp(32): warning C4624: 'B<int>': destructor was implicitly defined as deleted
怎么了?我错过了什么吗?还是那里的评论和答案有误??
修改成这样就可以了
template<typename T>
class B : public A<T>
{
public:
using typename A<T>::Vec_t;
// .........
};
在 C++ 中,如果 A
是模板,则独立 A
不是 'complete' 类型。您需要指定模板参数。这就是 A<T>
解决它的原因。
我认为混淆是由 A
用作基础 class 的方式引起的。如果 class 模板派生自带有模板参数的 class 模板,则必须完全限定基本 class 名称。但是如果 class 派生自 class 模板特化,您可以使用没有模板参数列表的基础 class 名称。
template<typename T>
struct A {
using t = T;
};
template<typename T>
struct B : A<T> {
using typename A<T>::t; // Full qualification needed -> mandatory argument list
};
struct C : A<int> {
using typename A::t; // Injected class name -> optional argument list
};
另请注意,t
可直接在 C
中使用,无需任何 using 声明或 typedef。我可能仍然有用,例如,如果 C
私下继承自 A<int>
,但您希望 t
在 C
中公开可用(在示例中,C
默认公开继承,因为它是 struct
).
这个问题不是重复的,而是跟进 Propagating 'typedef' from based to derived class for 'template'
作为继承 typedef 的解决方案,建议使用 using
将它们导入派生的 class,而简单的 using typename Base::typedefed_type
应该就足够了。
以下代码大部分摘自Roman Kruglov's answer:
#include <vector>
template<typename T>
class A
{
public:
typedef std::vector<T> Vec_t;
};
template<typename T>
class B : public A<T>
{
public:
using typename A::Vec_t;
// .........
private:
Vec_t v;
};
int main()
{
B<int> bb;
}
但是编译失败,因为编译器非常想要 A
.
英特尔编译器错误信息:
1>C:\Work\EDPS\test_eigen\test_eigen.cpp(27): error : argument list for class template "A" is missing
1> using typename A::Vec_t;
1> ^
1> detected during instantiation of class "B<T> [with T=int]" at line 34
1>
1>C:\Work\EDPS\test_eigen\test_eigen.cpp(31): error : identifier "Vec_t" is undefined
1> Vec_t v;
1> ^
1> detected during instantiation of class "B<T> [with T=int]" at line 34
1>
MVC 错误信息:
c:\work\edps\test_eigen\test_eigen.cpp(27): error C2955: 'A': use of class template requires template argument list
1>c:\work\edps\test_eigen\test_eigen.cpp(17): note: see declaration of 'A'
1>c:\work\edps\test_eigen\test_eigen.cpp(32): note: see reference to class template instantiation 'B<T>' being compiled
1>c:\work\edps\test_eigen\test_eigen.cpp(27): error C3210: 'A': a member using-declaration can only be applied to a base class member
1>c:\work\edps\test_eigen\test_eigen.cpp(32): warning C4624: 'B<int>': destructor was implicitly defined as deleted
怎么了?我错过了什么吗?还是那里的评论和答案有误??
修改成这样就可以了
template<typename T>
class B : public A<T>
{
public:
using typename A<T>::Vec_t;
// .........
};
在 C++ 中,如果 A
是模板,则独立 A
不是 'complete' 类型。您需要指定模板参数。这就是 A<T>
解决它的原因。
我认为混淆是由 A
用作基础 class 的方式引起的。如果 class 模板派生自带有模板参数的 class 模板,则必须完全限定基本 class 名称。但是如果 class 派生自 class 模板特化,您可以使用没有模板参数列表的基础 class 名称。
template<typename T>
struct A {
using t = T;
};
template<typename T>
struct B : A<T> {
using typename A<T>::t; // Full qualification needed -> mandatory argument list
};
struct C : A<int> {
using typename A::t; // Injected class name -> optional argument list
};
另请注意,t
可直接在 C
中使用,无需任何 using 声明或 typedef。我可能仍然有用,例如,如果 C
私下继承自 A<int>
,但您希望 t
在 C
中公开可用(在示例中,C
默认公开继承,因为它是 struct
).