Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to concatenate std::is_same in a struct

Tags:

c++

templates

My enable_if statement was getting very long so I wanted to somehow typedef. I am not sure what the best way is though.

I tried this, but it doesn't work

template<typename T>
struct isValidImageFormat
{
    typedef 
        typename std::is_same<T, float>::value || 
        typename std::is_same<T, unsigned char>::value 
        value;
};

The error is:

expected unqualified-id before »||« token typename std::is_same::value ||

Questions:

  • What is wrong with my code?
  • What would be a good solution to my Problem?
like image 868
NOhs Avatar asked Nov 30 '22 22:11

NOhs


2 Answers

You want std::disjunction (the fancy word in philosophy for "or"):

typedef std::disjunction<
    std::is_same<T, float>, 
    std::is_same<T, unsigned char>> condition;

Then you can use condition::value to get a true or false value. Or if you only want a value, try this:

constexpr bool condition =
    std::is_same<T, float>::value || 
    std::is_same<T, unsigned char>::value;
like image 170
John Zwinck Avatar answered Dec 04 '22 14:12

John Zwinck


The typename keyword is used when you want to work with types, in your example you want to work with constexpr bool value.

template<typename T>
struct isValidImageFormat
{
    constexpr static bool value =  
       std::is_same<T, float>::value || 
       std::is_same<T, unsigned char>::value;
};
like image 20
Andrzej Budzanowski Avatar answered Dec 04 '22 13:12

Andrzej Budzanowski