﻿# std::common\_type

## Definition

_std::common\_type_ is a trait from the standard library\. This trait allows you to pass an arbitrary number of types, processes them, and outputs their common type\. _std::common\_type_ became first available in the C\+\+11 standard\. When obtaining a common type, _std::common\_type_ relies upon the conditional operator and does some conversions we'll review below\.

## Possible implementation

```cpp
namespace std
{
template <typename ...>
struct common_type;                                      // (1)

template <typename ...Ts>
using common_type_t = typename common_type<Ts...>::type;

template <>
struct common_type<>                                     // (2)
{
};

template <class T>
struct common_type<T>                                    // (3)
{
  using type = std::decay_t<T>;
};

template <class T, class U>
struct common_type<T, U>                                 // (4)
{
  using type = decay_t<decltype( true ? declval< std::decay_t<T> >()
                                      : declval< std::decay_t<U> >() ) >;
};

template <class T, class U, class ...V>
struct common_type<T, U, V...>                           // (5)
{
  using type = typename common_type<typename common_type<T, U>::type,
                                    V...>::type;
};
}
```

It's worth mentioning that _common\_type_ is implemented in the standard library in a similar way\. Now let's examine the code above and see what happens there:

1. The primary variadic class template is declared\.
1. For an empty list of template arguments, we declare the explicit template specialization that does not contain anything\.
1. For one template argument, we declare the partial template specialization that contains this type after the [_std::decay_](https://en.cppreference.com/w/cpp/types/decay) trait is performed\. This trait removes CV\-qualifiers, links, adds pointers for functions \(_function\-to\-pointer conversion_\), and converts arrays to pointers \(_array\-to\-pointer conversion_\)\.
1. For two template arguments, we declare the partial specialization that infers the resulting type based on the type inference rules of the conditional operator, applying the _std::decay_ trait to the passed arguments beforehand\.
1. For three or more template arguments, we declare the partial specialization that first retrieves the common type for the first two arguments\. It uses the specialization for 2 types to do this\. Then it instantiates itself recursively, passing the common type for the first pair of types and the rest of the template parameter pack as template arguments\. Overall, _common\_type<a, b, c, d\>_ is equivalent to _common\_type<common\_type<common\_type<a, b\>, c\>, d\>_\. See an example on [C\+\+ Insights](https://cppinsights.io/lnk?code=I2luY2x1ZGUgPHR5cGVfdHJhaXRzPgoKdGVtcGxhdGUgPHR5cGVuYW1lIC4uLj4Kc3RydWN0IGNvbW1vbl90eXBlOyAvLyAoMSkKCnRlbXBsYXRlIDx0eXBlbmFtZSAuLi5Ucz4KdXNpbmcgY29tbW9uX3R5cGVfdCA9IHR5cGVuYW1lIGNvbW1vbl90eXBlPFRzLi4uPjo6dHlwZTsKCnRlbXBsYXRlIDw+CnN0cnVjdCBjb21tb25fdHlwZTw+IC8vICgyKQp7Cn07Cgp0ZW1wbGF0ZSA8Y2xhc3MgVD4Kc3RydWN0IGNvbW1vbl90eXBlPFQ+IC8vICgzKQp7CiAgdXNpbmcgdHlwZSA9IHN0ZDo6ZGVjYXlfdDxUPjsKfTsKCnRlbXBsYXRlIDxjbGFzcyBULCBjbGFzcyBVPgpzdHJ1Y3QgY29tbW9uX3R5cGU8VCwgVT4gICAgICAgICAvLyAoNCkKewogIHVzaW5nIHR5cGUgPSBzdGQ6OmRlY2F5X3Q8CiAgICAgIGRlY2x0eXBlKCB0cnVlID8gc3RkOjpkZWNsdmFsPCBzdGQ6OmRlY2F5X3Q8VD4gPigpCiAgICAgICAgICAgICAgICAgICAgIDogc3RkOjpkZWNsdmFsPCBzdGQ6OmRlY2F5X3Q8VT4gPigpICkgPjsKfTsKCnRlbXBsYXRlIDxjbGFzcyBULCBjbGFzcyBVLCBjbGFzcy4uLiBWPgpzdHJ1Y3QgY29tbW9uX3R5cGU8VCwgVSwgVi4uLj4gICAgICAgICAvLyAoNSkKewogIHVzaW5nIHR5cGUgPSB0eXBlbmFtZSBjb21tb25fdHlwZTx0eXBlbmFtZSBjb21tb25fdHlwZTxULFU+Ojp0eXBlLCBWLi4uPjo6dHlwZTsKfTsKCnRlbXBsYXRlCnN0cnVjdCBjb21tb25fdHlwZTxjaGFyLCBzaG9ydCwgaW50Pjs=&insightsOptions=cpp2a&std=cpp2a&rev=1.0)\.

Helpful links

* [A description of the conditional operator behavior](https://timsong-cpp.github.io/cppwp/n4868/expr.cond), draft N4868 \(C\+\+20\)
* [A description of the std::common\_type behavior](https://timsong-cpp.github.io/cppwp/n4868/meta.trans.other), draft N4868 \(C\+\+20\)