Victor Martin
Victor Martin

Reputation: 89

Problem with basic usage of std::enable_if

I'm trying to make a simple template function that given some parameters, it outputs them with a space in between. Some of those can be elements of an enum, and in that case I want to output its integer value.

This is the code I have:

#include <iostream>
#include <type_traits>
using std::cerr;
using std::endl;

// Output integer value if parameter is an element of an enum
template<typename T, typename = typename std::enable_if_t<std::is_enum<T>::value>>
constexpr std::ostream& debug(const T& a) {
  cerr << (int)(a);
  return cerr;
}

// Output parameter otherwise
template<typename T, typename = typename std::enable_if_t<!std::is_enum<T>::value>>
constexpr std::ostream& debug(const T& a) {
  cerr << a;
  return cerr;
}

// Print parameters separated by a space
template<typename T, typename ...U>
constexpr std::ostream& debug(const T& a, const U&... b) {
  debug(a) << ' ';
  debug(b...);
  return cerr;
}

template<typename ...U>
constexpr void debug(const U&... a) {
  debug(a...) << std::endl;
}

enum class Animal{Dog, Cat};

int main() {
  debug(Animal::Dog);
  debug(Animal::Dog, Animal::Cat);
  debug("Hello");
  debug(100, 'A');
  debug(Animal::Dog, "Hello", "Bye");
}

Commenting the last three lines it says that the second function is a redeclaration of the first. Any ideas on how to solve this or why it does not work?

Upvotes: 2

Views: 495

Answers (1)

VLL
VLL

Reputation: 10155

Default template arguments are not part of the function definition. Use a dummy parameter instead, so that the second argument has a different type:

template<typename T, typename std::enable_if_t<std::is_enum<T>::value, int> = 0>
constexpr std::ostream& debug(const T& a) {
  std::cerr << (int)(a);
  return std::cerr;
}

template<typename T, typename std::enable_if_t<!std::is_enum<T>::value, int> = 0>
constexpr std::ostream& debug(const T& a) {
  std::cerr << a;
  return std::cerr;
}

Upvotes: 3

Related Questions