RainingChain
RainingChain

Reputation: 7746

Why is std::is_same not working with bool

#include <array>
template<typename T>
void Func(T Param)
   {
   int Val = 0;
   if (std::is_same<T, bool>::value)
      Val += Param ? 1 : 0;
   }

int main() 
   {
   std::array<int, 10> A;
   Func(A);
   return 0;
   }

When I compile with gcc or MSVC, I get:

Error C2440 '?': cannot convert from 'std::array<int,10>' to 'bool'

Shouldn't the compiler not even compile Val += Param ? 1 : 0; because std::is_same<std::array<int, 10>, bool>::value is 0?

Upvotes: 1

Views: 1684

Answers (1)

Edgar Rokjān
Edgar Rokjān

Reputation: 17483

In your current scenario, when the compiler tries to instantiate Func template, it sees:

Val += Param ? 1 : 0;

where Param has a type of std::array<int, 10>, so it complains.

The thing is that if clause with std::is_same does not remove a part of your code magically during the function template instantiation.

Since C++17 you might use if constexpr:

if constexpr (std::is_same_v<T, bool>)
   Val += Param ? 1 : 0;
}

which solves the problem.

Before C++17 you may do some experiments with tag dispatching. For instance, something like should work:

template<typename T>
auto get(const T& p, std::true_type) {
    return p ? 1 : 0;
}

template<typename T>
auto get(const T& p, std::false_type) {
    return 0;
}

And then:

template<typename T>
void Func(T Param)
{
    int Val = 0;        
    // ...        
    Val += get(Param, std::is_same<T, bool>{});
}

Wandbox example

Upvotes: 7

Related Questions