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.
If you want to take into account cv-qualifiers, you would just need to specialise it a little more:
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:
#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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.