如何知道类型是否是std :: vector的特化? [英] How to know if a type is a specialization of std::vector?

查看:76
本文介绍了如何知道类型是否是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.

Some kind of is_base_of for templates.

这样的东西存在吗?

推荐答案

在C ++ 11中,您还可以采用更通用的方式做到这一点:

In C++11 you can also do it in a more generic way:

#include <type_traits>
#include <iostream>

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的特化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆