c++ - 检查类型是否是模板的实例化

标签 c++ templates c++17 auto

我想在编译期间检查一个类型是否是特定模板的实例化。

例如:

  1. std::vector<int>std::vector 的实例化
  2. std::array<int, 5>std::array 的实例化

我可以进行适用于案例 1 但不适用于案例 2 的测试。

#include <iostream>
#include <type_traits>
#include <string>
#include <vector>
#include <array>
#include <queue>
template<template<typename...> class, typename...>
struct is_instantiation : public std::false_type {};

template<template<typename...> class U, typename... T>
struct is_instantiation<U, U<T...>> : public std::true_type {};

int main() {
    using A = std::vector<int>;
    std::cout << is_instantiation<std::vector, A>::value << "\n";
    std::cout << is_instantiation<std::queue, A>::value << "\n";
    // std::cout << is_instantiation<std::array, A>::value << "\n";
}

如何让它适用于这两种情况?

我试过自动,但不能让它工作。

Advantages of auto in template parameters in C++17

最佳答案

特殊化 std::array 大小

我看到的唯一方法是制作具有预定义数组大小的专用 Array 类。像这样的:

#include <iostream>
#include <type_traits>
#include <string>
#include <vector>
#include <array>
#include <queue>
template<template<typename...> class, typename...>
struct is_instantiation : public std::false_type {};

template<template<typename...> class U, typename... T>
struct is_instantiation<U, U<T...>> : public std::true_type {};

template <class T> class My5Array {
    public:
    My5Array() { }
    private:
    std::array<T, 5> arr;
};

template <class T> class My10Array {
    public:
    My10Array() { }
    private:
    std::array<T, 10> arr;
};

int main() {
    using A = std::vector<int>;
    using B = My5Array<int>;
    std::cout << is_instantiation<std::vector, A>::value << "\n";
    std::cout << is_instantiation<std::queue, A>::value << "\n";
    std::cout << is_instantiation<My5Array, A>::value << "\n";
    std::cout << is_instantiation<My5Array, B>::value << "\n";
    std::cout << is_instantiation<My10Array, B>::value << "\n";
}

打印

1
0
0
1
0

当然也有缺点:

  • 由于数组大小固定,可能会浪费内存
  • 所需的数组大小需要多个类
  • 非标准类型MyXArray的使用
  • 显然 My5Array 的一个实例不能同时是 My10Array 的一个实例(参见上面代码中的 var B)

第一种可能的选择:std::dynarray

我还找到了 std::dynarray,它可以代替 std::array 工作,但我认为它尚未包含在最新的 C++ 标准中。也许值得关注。

第二种可能的选择:让它放下

可用的标准容器可能足以满足大多数应用程序。

关于c++ - 检查类型是否是模板的实例化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50284646/

相关文章:

c# - 以编程方式更改屏幕色彩平衡

c++ - QT中如何加入url

c++ - "No rule to make target ` distclean '"使用 libtool 便利库时

基类的c++模板特化

c++ - Clang 说 is_same_v<int, double> 的用法不是 constexpr,不知道为什么不是

c++ - c 和 c++ 共享带有命名空间的 h 文件

c++ - 带有前向声明的默认模板参数

c++ - 模板元编程示例没有意义

c++ - 如何获取可变参数宏中每个参数的名称?

c++ - 使用 std::Optional<A> 作为 A 类的类成员