前向声明的类型模板可以参与模板专业化吗?

Can forward declared type templates participate in template specialization ?

以下不编译

#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;
}

我想确保前向声明的类型模板(在 vector 之上)在专业化上下文中实际上是不可使用的,并且这不是一个错误的实现。另外 为什么会这样? 我不想创建类型的对象 vector<> 仅将其用作在专业化之间分派的标签; 在实例化点包含 <vector> 是否足够(is_vector<> 的调用站点)

前向声明标准库容器is undefined behavior(如果你设法编译它),所以对于std::vector你必须在定义is_vector之前#include <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 {}; 
}