C++ 转发声明的类型模板能否参与模板专门化?
以下内容不可编译C++ 转发声明的类型模板能否参与模板专门化?,c++,templates,template-meta-programming,C++,Templates,Template Meta Programming,以下内容不可编译 #include <iostream> #include <type_traits> // forward declaration of a type template template<class T, class Alloc = std::allocator<T>> class std::vector; template<class T> struct is_vector : std::false_type
#include <iostream>
#include <type_traits>
// forward declaration of a type template
template<class T, class Alloc = std::allocator<T>> class std::vector;
template<class T>
struct is_vector : std::false_type { };
// using the forward declared type template
template<class T, class Alloc>
struct is_vector<std::vector<T, Alloc>> : std::true_type { };
#include <vector>
int main()
{
std::cout << is_vector<std::vector<int>>::value << std::endl;
}
#包括
#包括
//类型模板的转发声明
模板类std::vector;
模板
结构是_向量:std::false_类型{};
//使用前向声明的类型模板
模板
结构是_向量:std::true_类型{};
#包括
int main()
{
std::cout转发标准库容器的声明(如果您设法编译它),因此对于std::vector
,您必须在定义is\u vector
之前包含
对于您自己的类型,您可以这样做:
// forward declaration of a type template
namespace my {
template<class T, class Alloc> class vector;
}
template<class T>
struct is_vector : std::false_type { };
// using the forward declared type template
template<class T, class Alloc>
struct is_vector<my::vector<T, Alloc>> : std::true_type { };
namespace my {
template<class T, class Alloc = std::allocator<T>> class vector {};
}
//类型模板的转发声明
名称空间我的{
模板类向量;
}
模板
结构是_向量:std::false_类型{};
//使用前向声明的类型模板
模板
结构是_向量:std::true_类型{};
名称空间我的{
模板类向量{};
}
转发标准库容器的声明(如果您设法编译它),因此对于std::vector
,您必须在定义is\u vector
之前包含
对于您自己的类型,您可以这样做:
// forward declaration of a type template
namespace my {
template<class T, class Alloc> class vector;
}
template<class T>
struct is_vector : std::false_type { };
// using the forward declared type template
template<class T, class Alloc>
struct is_vector<my::vector<T, Alloc>> : std::true_type { };
namespace my {
template<class T, class Alloc = std::allocator<T>> class vector {};
}
//类型模板的转发声明
名称空间我的{
模板类向量;
}
模板
结构是_向量:std::false_类型{};
//使用前向声明的类型模板
模板
结构是_向量:std::true_类型{};
名称空间我的{
模板类向量{};
}
您的转发声明是错误的。您的转发声明是错误的。我在VS2012中编译了一个有问题的代码变体(在gcc中失败),因此对整个情况感到困惑。对于InfoNX,我在VS2012中编译了一个有问题的代码变体(在gcc中失败),所以对整个情况感到困惑。Thnx获取信息