久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

做一個 static_assert 模板類型是另一個模板

Doing a static_assert that a template type is another template(做一個 static_assert 模板類型是另一個模板)
本文介紹了做一個 static_assert 模板類型是另一個模板的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我如何static_assert喜歡這個?如果不是 C++ 或 C++11 中的新特性,也許 Boost 支持它?

How do I static_assert like this? Maybe Boost supports it if not C++ or new features in C++11?

template<T>
struct foo {};

template<FooType>
struct bar {
  static_assert(FooType is indeed foo<T> for some T,"failure"); //how?
};

推薦答案

您可以按照這些思路做一些事情.給定一個可以驗證類是否是類模板的實例的特征:

You could do something along these lines. Given a trait that can verify whether a class is an instantiation of a class template:

#include <type_traits>

template<typename T, template<typename> class TT>
struct is_instantiation_of : std::false_type { };

template<typename T, template<typename> class TT>
struct is_instantiation_of<TT<T>, TT> : std::true_type { };

在您的程序中按如下方式使用它:

Use it as follows in your program:

template<typename T>
struct foo {};

template<typename FooType>
struct bar {
  static_assert(is_instantiation_of<FooType, foo>::value, "failure");
};

int main()
{
    bar<int> b; // ERROR!
    bar<foo<int>> b; // OK!
}

如果需要,您可以將其概括為檢測類是否是具有任意數(shù)量(類型)參數(shù)的模板的實例,如下所示:

If you want, you could generalize this to detect whether a class is an instance of a template with any number of (type) parameters, like so:

#include <type_traits>

template<template<typename...> class TT, typename T>
struct is_instantiation_of : std::false_type { };

template<template<typename...> class TT, typename... Ts>
struct is_instantiation_of<TT, TT<Ts...>> : std::true_type { };

template<typename FooType>
struct bar {
  static_assert(is_instantiation_of<foo, FooType>::value, "failure");
};

然后你會在你的程序中這樣使用它:

You would then use it this way in your program:

template<typename FooType>
struct bar {
  static_assert(is_instantiation_of<foo, FooType>::value, "failure");
};

int main()
{
    bar<int> b; // ERROR!
    bar<foo<int>> b; // OK!
}

這是一個noliver>noliver.

Here is a live example.

這篇關于做一個 static_assert 模板類型是另一個模板的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權益,請聯(lián)系我們刪除處理,感謝您的支持!

相關文檔推薦

Difference between std::reference_wrapper and simple pointer?(std::reference_wrapper 和簡單指針的區(qū)別?)
Difference between const. pointer and reference?(常量之間的區(qū)別.指針和引用?)
How to access the contents of a vector from a pointer to the vector in C++?(c++ - 如何從指向向量的指針訪問向量的內(nèi)容?)
Meaning of *amp; and **amp; in C++(*amp; 的含義和**amp;在 C++ 中)
Why can#39;t I do polymorphism with normal variables?(為什么我不能對普通變量進行多態(tài)?)
Dereferencing deleted pointers always result in an Access Violation?(取消引用已刪除的指針總是會導致訪問沖突?)
主站蜘蛛池模板: 2019天天操 | 亚洲精品免费在线观看 | 亚洲欧美一区二区三区视频 | 亚欧精品一区 | 国产清纯白嫩初高生视频在线观看 | 国产亚洲欧美日韩精品一区二区三区 | 91国内在线观看 | 精品欧美黑人一区二区三区 | 国产精品精品 | 综合久久综合久久 | 9999久久 | 午夜精品久久久久久不卡欧美一级 | 亚洲一区二区三区在线播放 | 国产精品久久久久久久一区二区 | 中文字幕视频在线免费 | 亚洲97| 男女视频免费 | 99精品久久 | 91香蕉嫩草 | 黄色网址大全在线观看 | 中文字幕在线看第二 | 日本成人午夜影院 | 久久免费高清视频 | 2020亚洲天堂 | 亚洲国产成人精品女人久久久野战 | 日韩成人在线电影 | 国产精品精品3d动漫 | 天天插天天射天天干 | 国产中文字幕网 | 四虎影院新地址 | 高清一区二区视频 | 日韩在线视频一区 | 激情av在线 | 在线观看中文字幕dvd播放 | 蜜桃精品在线 | 国产一区二区高清在线 | 精品国产黄a∨片高清在线 成人区精品一区二区婷婷 日本一区二区视频 | 91视频在线观看 | 午夜精品福利视频 | 嫩草91在线| 久久久久久高潮国产精品视 |