15

This short C++17 program:

#include <iostream>

template <typename T> void output(T x)
{
    if constexpr (std::is_integral<decltype(x)>::value) {
        std::cout << static_cast<int>(x) << " is integral" << std::endl;
    } else {
        std::cout << x << " is not integral" << std::endl;
    }
}

int main()
{
    char x = 65;
    output(x);

    bool t = true;
    output(t);

    return 0;
}

Has this output:

65 is integral
1 is integral

In the template function named output, how can one detect that the argument x is boolean and not a number?

The plan is to output the value with std::cout << std::boolalpha <<, but only if the type is bool.

0

2 Answers 2

22

std::is_integral checks if a type is one of the following types: bool, char, char16_t, char32_t, wchar_t, short, int, long, long long (source). If you want to check if a type is the same as another type, std::is_same can be used. Both can be combined to get the wanted result:

template <typename T> void output(T x)
{
    if constexpr (std::is_integral<decltype(x)>::value && !std::is_same<decltype(x), bool>::value) {
        std::cout << static_cast<int>(x) << " is integral but not a boolean" << std::endl;
    } else {
        std::cout << x << " is not integral" << std::endl;
    }
}

or, since we already know the type of decltype(x), which is T:

template <typename T> void output(T x)
{
    if constexpr (std::is_integral<T>::value && !std::is_same<T, bool>::value) {
        std::cout << static_cast<int>(x) << " is integral but not a boolean" << std::endl;
    } else {
        std::cout << x << " is not integral" << std::endl;
    }
}

Another way can be to use a template specialization. This makes sure the other overload is being used to handle the boolean value.

template <typename T> void output(T x)
{
    if constexpr (std::is_integral<T>::value) {
        std::cout << static_cast<int>(x) << " is integral but not a boolean" << std::endl;
    } else {
        std::cout << x << " is not integral" << std::endl;
    }
}

template <> void output(bool x)
{
    std::cout << x << " is a boolean" << std::endl;
}
1
  • maybe change the output to "is integral but not bool" / "is bool or not integral" Commented Aug 15, 2018 at 13:09
2
namespace fmt {
  namespace adl {
    template<class T>
    void output( std::ostream& os, T const& t ) {
      os << t;
    }
    void output( std::ostream& os, bool const& b ) {
      auto old = os.flags();
      os << std::boolalpha << b;
      if (!( old & std::ios_base::boolalpha) )
        os << std::noboolalpha; // restore state
    }
    template<class T>
    void output_helper( std::ostream& os, T const& t ) {
      output(os, t); // ADL
    }
  }
  template<class T>
  std::ostream& output( std::ostream& os, T const& t ) {
    adl::output_helper( os, t );
    return os;
  }
}

now fmt::output( std::cout, true ) prints true, while fmt::output( std::cout, 7 ) prints 7.

You can extend fmt::output by creating a function in either fmt::adl or in the type T's namespace called output that takes a std::ostream& and a T const&.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.