本文介紹了如何知道類型是否是 std::vector 的特化?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
限時送ChatGPT賬號..
我整個上午都在解決這個問題,但沒有任何結果.基本上,我需要一個簡單的元編程東西,如果傳遞的參數是一種 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 的特化?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!