How to know if a type is a specialization of std::vector?(如何知道类型是否是 std::vector 的特化?)
问题描述
我整个上午都在解决这个问题,但没有任何结果.基本上,我需要一个简单的元编程东西,如果传递的参数是一种 std::vector 或不是,它允许我分支到不同的专业化.
I've been on this problem all morning with no result whatsoever.
Basically, I need a simple metaprogramming thing that allows me to branch to different specializations if the parameter passed is a kind of std::vector or not.
某种用于模板的is_base_of.
这种东西存在吗?
推荐答案
在 C++11 中,您也可以采用更通用的方式:
In C++11 you can also do it in a more generic way:
#include <type_traits>
#include <iostream>
#include <vector>
#include <list>
template<typename Test, 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()
{
typedef std::vector<int> vec;
typedef int not_vec;
std::cout << is_specialization<vec, std::vector>::value << is_specialization<not_vec, std::vector>::value;
typedef std::list<int> lst;
typedef int not_lst;
std::cout << is_specialization<lst, std::list>::value << is_specialization<not_lst, std::list>::value;
}
这篇关于如何知道类型是否是 std::vector 的特化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何知道类型是否是 std::vector 的特化?
基础教程推荐
- 这个宏可以转换成函数吗? 2022-01-01
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 我有静态或动态 boost 库吗? 2021-01-01
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 常量变量在标题中不起作用 2021-01-01
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
