为什么通用专业化特征不适用于 std::array
Why doesn't the generic specialization trait apply to std::array
我发现有趣的是,这个类型trait不匹配std::array(它给出一个编译错误),但它适用于unordered_map。为什么呢?
#include <iostream>
#include <unordered_map>
#include <array>
template <typename T, template <typename...> class Ref>
struct is_specialization : std::false_type {
};
template <template <typename...> class Ref, typename... Args>
struct is_specialization<Ref<Args...>, Ref> : std::true_type {
};
int main()
{
using T = std::unordered_map<int, int>;
using C = std::array<int, 2>;
auto value = is_specialization<T, std::unordered_map>::value;
std::cout << "Is type of unorderd map specialization? : " << std::boolalpha << value << std::endl;
auto secondValue = is_specialization<C , std::array>::value;
std::cout << "Is type of array specialization? : " << std::boolalpha << secondValue << std::endl;
}
主模板接受两个实参:一个类型实参和一个模板模板形参,后者的模板实参都是类型:
template <typename T, template <typename...> class Ref>
struct is_specialization;
std::array
是一个类模板,是的,但是它的模板参数不是所有类型:
template<
class T,
std::size_t N // <== not a type
> struct array;
在模板元编程领域,任何不是类型的东西都是二等公民。这只是价值观糟糕的一个例子。
如果您编写了自己的array
包装器,并接受两种类型:
template <typename T, typename N>
struct my_array : std::array<T, N::value> { };
那么你就可以按照你的期望使用你的特质了:
using C = my_array<int, std::integral_constant<int, 2>>;
auto secondValue = is_specialization<C , my_array>::value; // true
相关文章:
- 为什么 std::erase(std::erase_if) 不是适用于<algorithm>任何容器的模板?
- 使输出流式处理运算符适用于 boost::variant<std::vector<int>、int、double 的正确方法是什么>
- 结构化绑定是否适用于 std::vector?
- 没有适用于 std::unique_ptr 的适当默认构造函数
- 适用于 std::unique_ptr 的内存高效自定义删除器?
- std::string 是否仅适用于 c++ 中的'std::cin'
- 为什么不区分大小写适用于 std::unordered_set的 std::hash 函数?
- 为什么我的"choose k from n"算法适用于 std::vector,而不适用于 std::map?
- 无法获得 boost::spirit parser&lexer 适用于 std::string 或 int 或 double 以外的令牌类型
- 重载分辨率如何适用于 std::vector:<int>:insert
- 如何将上下文信息传递给自定义运算符<<适用于 std::iostream
- 在C++std::streams中,失败后,如何获得失败原因?必需:线程安全,适用于Windows和Linux(或至少M
- 适用于 MacOS 的"-std=gnu++0x"选项
- 警告:扩展初始值设定项列表仅适用于-std=c++0x或-std=gnu++0x
- 警告:扩展初始化器列表仅适用于STD c++ 11
- 自定义迭代器适用于std::sort,但不适用于tbb::parallel_sort
- 不在异常中嵌入std::字符串的规则是否仍然适用于move构造函数
- boost::variant是否适用于std::string
- 如何包装对"std::thread"构造函数的调用?(适用于gcc、VS和icpc)
- 可变参数模板转换为 std::function<R(ARGS...)>适用于 GCC 而不是 MSVC2013,为什么?