1

Implicit type conversion rules in C++ operators

Lists the implicit type conversion for C++.

So the question is, is there a type trait that can do that table? something akin to

template <typename T, typename J>
struct promotion_type
{
  typedef decltype(operator+(const T&,const J&)) type;
};

(Not my question, but this doesn't compile:

/home/user/source/testdir/main.cpp:97:51: error: there are no arguments to 'operator+' that depend on a template parameter, so a declaration of 'operator+' must be available [-fpermissive]
   typedef decltype(operator+(const T&,const J&)) type;
                                               ^
/home/user/source/testdir/main.cpp:97:51: note: (if you use '-fpermissive', G++ will accept your code, but allowing the use of an undeclared name is deprecated)

)

Community
  • 1
  • 1
IdeaHat
  • 7,641
  • 1
  • 22
  • 53

1 Answers1

3

How about decltype(std::declval<T>() + std::declval<J>())?

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • Interesting...i just used that to verify that std::common_type wasn't good enough. – IdeaHat Apr 03 '14 at 22:21
  • @MadScienceDreams The only difference is that bool, char, and short are all promoted to int by `+`, but not by common_type if T==J. The reference implementation of common_type is `decltype(true ? declval() : declval())`. – Oktalist Apr 03 '14 at 23:17