我想部分专门化一个包含基本类型的std :: vector的类模板。
我的方法看起来像这样,但是does not compile
#include <type_traits>
#include <vector>
#include <string>
#include <iostream>
template <typename T, bool bar = false>
struct foo {
static void show()
{
std::cout << "T" << std::endl;
}
};
template <typename T>
struct foo<typename std::enable_if<std::is_fundamental<T>::value, std::vector<T>>::type, false> {
static void show()
{
std::cout << "std::vector<fundamental type>" << std::endl;
}
};
template <typename T>
struct foo<std::vector<T>, false> {
static void show()
{
std::cout << "std::vector<T>" << std::endl;
}
};
int main()
{
foo<int>::show();
foo<std::vector<int>>::show();
foo<std::vector<std::string>>::show();
}
我怎样才能让它发挥作用?
答案 0 :(得分:2)
template <typename T, typename = void>
struct foo
{
static void show()
{
std::cout << "T" << std::endl;
}
};
template <typename T, typename Alloc>
struct foo<std::vector<T, Alloc>
, typename std::enable_if<!std::is_fundamental<T>::value>::type>
{
static void show()
{
std::cout << "std::vector<T>" << std::endl;
}
};
template <typename T, typename Alloc>
struct foo<std::vector<T, Alloc>
, typename std::enable_if<std::is_fundamental<T>::value>::type>
{
static void show()
{
std::cout << "std::vector<fundamental type>" << std::endl;
}
};