14

Just to see how it worked, I looked at the libstdc++ implementation of std::common_type in the header type_traits. I have to admit that I don't really understand how it works. Here it is:

/// common_type
template<typename... _Tp>
    struct common_type;

template<typename _Tp>
    struct common_type<_Tp>
    { typedef _Tp type; };

template<typename _Tp, typename _Up>
    struct common_type<_Tp, _Up>
    { typedef decltype(true ? declval<_Tp>() : declval<_Up>()) type; };

template<typename _Tp, typename _Up, typename... _Vp>
    struct common_type<_Tp, _Up, _Vp...>
    {
        typedef typename
        common_type<typename common_type<_Tp, _Up>::type, _Vp...>::type type;
    };

I understand well how the first, second and fourth declarations work. However, I can't manage to understand how the third declaration works. Could someone try to explain the mechanism used here?

3 Answers 3

14

First off, std::declval<T>() yields an r-value of type T. Trying to do anything with the value will fail so it can only be used in an unevaluated context. Next, the ternary operator deduces its type as most specialized type common to both arguments (if there is no such type, it fails). So, the type of the expression

true? declval<T0>(): declval<T1>()

is the most specialized common type of T0 and T1. All what remains is to turn this expression into a type and making sure that it isn't evaluated. decltype(expr) does just this. Clearly, the two argument version of the beef of the logic: the others are there to deal with the corner case (one argument) and to leverage the two argument version to yield the common type of arbitrary types.

4

The third version uses the conditional operator to determine the common type. Its rules are described at quite a length in section 5.16 of the standard, so I'm not sure I should copy them here.

Simply put, the expression:

boolean-expression ? second-operand : third-operand

has a "common type" of the second and third operands, if such exists. The decltype specifier is then used to "convert" the expression into a type-specifier.

2
  • 3
    +1, a nice read on magic including the ternary operator: Conditional Love: FOREACH redux Sep 5, 2012 at 21:38
  • Ok, I think I finally got the trick. So it relies on the fact that the ternary operator deduces its return types based on its operands. It did not think of that. Thanks!
    – Morwenn
    Sep 5, 2012 at 21:39
1

Long Story Short: The decltype is making the C++ compiler determine the closest ancestor type for it.

Tertiary Operator's have the resulting static type of the closest ancestor of the two possible expressions.

E.g:

A inherits from B

X inherits from Y inherits from B

<expression> ? <expression with static type A> : <expression with static type X> 
    = <expression with static type B>  // this is how the C++ parser sees it

This is how the C++ language works. The decltype just makes the typedef be the static type of the result of that expression (whatever type the C++ compiler determines it to be)

1
  • 1
    This is slightly misleading in the use of the terms ancestor and in the example using inheritance. Sure it does that, but it does much more, if one of the types is convertible to the other, it will choose the second type, for example (true? 1, 1.) has type double as int is convertible to double, although there is no ancestor or inheritance. Sep 5, 2012 at 21:45

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

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