dimanche 31 décembre 2017

conditional declaration of type using decltype

I thought that conditional type could be declared using decltype in template functions. But it seems not. Could anyone point out what's wrong with my test code?

#include <boost/type_index.hpp>
using boost::typeindex::type_id_with_cvr;

#define print_type(var) do { \
  std::cout << type_id_with_cvr<decltype(var)>().pretty_name() << std::endl; \
} while(0)

template <typename T1, typename T2>
auto max(T1 a, T2 b) -> decltype(a < b ? b : a) {
  decltype(a < b ? b : a) c = a < b ? b : a;
  print_type(c);
  return a < b ? b : a;
}

int main() {
  int i = 10;
  double d = 3.3;

  decltype(i < d? d : i) r = i < d? d : i;
  print_type(r); // -> double
  std::cout << r << std::endl; // 10
}

Aucun commentaire:

Enregistrer un commentaire