简体   繁体   English

如何比较高阶订单类型?

[英]How do I compare higher order types?

I want to have something like this: 我想要这样的东西:

std::cout << std::is_same<Buffer<int>, Buffer<float>>::value;

But instead of comparing the whole type I just want to know if the type is a buffer. 但是我没有比较整个类型,而是想知道类型是否是缓冲区。

std::cout << std::is_buffer<Buffer<int>>::value // true;
std::cout << std::is_buffer<Buffer<float>>::value // true;

Would this be possible? 这可能吗? Maybe with the help of templates of templates? 也许借助模板的模板?

Just make a simple trait: 只需做一个简单的特征:

#include <type_traits>

template<typename T>
struct is_buffer : std::false_type {};

template<typename T>
struct is_buffer<Buffer<T>> : std::true_type {};

This doesn't take into account cv-qualifiers though. 但是,这没有考虑cv限定词。

If you want to take into account cv-qualifiers, you would just need to specialise it a little more: 如果要考虑cv限定词,则只需对其进行专门化处理即可:

template<typename T>
struct is_buffer<const T> : is_buffer<T> {};

template<typename T>
struct is_buffer<volatile T> : is_buffer<T> {};

You can implement a trait class like the following: 您可以实现如下的trait类:

#include <type_traits>

template<class T> struct is_buffer : std::false_type {};

template<class T> struct is_buffer<Buffer<T> > : std::true_type {};
template<typename T>
class is_buffer : public std::false_type
{ };

template<typename T>
class is_buffer<Buffer<T>> : public std::true_type
{ };

So the is_buffer which inherits std::true_type will be used if the template argument is of type Buffer<T> , otherwise the std::false_type one will be used. 所以is_buffer它继承std::true_type如果模板参数的类型的将被用于Buffer<T>否则std::false_type一个将被使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM