久久久久久久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!
}

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

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模板網!

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

相關文檔推薦

Difference between std::reference_wrapper and simple pointer?(std::reference_wrapper 和簡單指針的區別?)
Difference between const. pointer and reference?(常量之間的區別.指針和引用?)
How to access the contents of a vector from a pointer to the vector in C++?(c++ - 如何從指向向量的指針訪問向量的內容?)
Meaning of *amp; and **amp; in C++(*amp; 的含義和**amp;在 C++ 中)
Why can#39;t I do polymorphism with normal variables?(為什么我不能對普通變量進行多態?)
Dereferencing deleted pointers always result in an Access Violation?(取消引用已刪除的指針總是會導致訪問沖突?)
主站蜘蛛池模板: 国产精品三级 | 中文字幕一区二区三区不卡在线 | 婷婷去俺也去 | 亚洲精品中文在线 | 精品一区二区三区四区五区 | 亚洲社区在线 | 99精品久久久国产一区二区三 | 国产1区2区3区 | 日韩国产在线 | 亚洲高清av| 成人一区在线观看 | 午夜精品久久久久久久久久久久久 | 狠狠婷婷综合久久久久久妖精 | 欧美精品一区二区三区在线 | 日本网站在线看 | 亚洲一区中文 | 精品亚洲一区二区三区 | 欧美日韩久久久 | 国产91视频一区二区 | 丝袜美腿一区二区三区动态图 | 玖玖爱365| 日韩精品成人 | 亚洲永久| 免费看a | 男人的天堂在线视频 | 成人在线视 | 国产亚洲精品久久午夜玫瑰园 | 久久久久久久久久久久91 | 日韩 国产 在线 | av黄在线观看| 精品国产伦一区二区三区观看体验 | 亚洲综合在线一区 | 亚洲九色| 不卡在线视频 | 在线中文字幕av | 国产精品亚洲一区二区三区在线 | 精品视频一区二区三区在线观看 | 亚洲免费网 | 日韩2020狼一二三 | 亚洲一级av毛片 | 日韩亚洲一区二区 |