# std::same_as

> C++ Atlas: `cppatlas.com/w/cpp/concepts/same_as` | Source: https://en.cppreference.com/w/cpp/concepts/same_as | Domain: Concepts | Standards: C++11, C++20

The concept same_as<T, U> is satisfied if and only if T and U denote the same type.

## Official Specification Summary (cppreference)

```cpp
// Defined in header <concepts>

template< class T, class U >
concept same_as = /* see below */; // (since C++20)
```

The concept `same_as<T, U>` is satisfied if and only if `T` and `U` denote the same type.

`std::same_as<T, U>` subsumes `std::same_as<U, T>` and vice versa.

### Possible implementation

```cpp
namespace detail
{
    template< class T, class U >
    concept SameHelper = std::is_same_v<T, U>;
}

template< class T, class U >
concept same_as = detail::SameHelper<T, U> && detail::SameHelper<U, T>;
```

### Example

```cpp
#include <concepts>
#include <iostream>

template<typename T, typename ... U>
concept IsAnyOf = (std::same_as<T, U> || ...);

template<typename T>
concept IsPrintable = std::integral<T> || std::floating_point<T> ||
    IsAnyOf<std::remove_cvref_t<std::remove_pointer_t<std::decay_t<T>>>, char, wchar_t>;

void println(IsPrintable auto const ... arguments)
{
    (std::wcout << ... << arguments) << '\n';
}

int main()
{
    println("Example: ", 3.14, " : ", 42, " : [", 'a', L'-', L"Z]");
}
```

Output:

```text
Example: 3.14 : 42 : [a-Z]
```

### References

- C++23 standard (ISO/IEC 14882:2024):

  - 18.4.2 Concept `same_as` [concept.same]

- C++20 standard (ISO/IEC 14882:2020):

  - 18.4.2 Concept `same_as` [concept.same]

### See also

- [`is_same`](https://en.cppreference.com/w/cpp/types/is_same) (C++11): checks if two types are the same (class template)

---
Attribution: content from https://en.cppreference.com/w/cpp/concepts/same_as (cppreference.com), licensed under CC-BY-SA 3.0 and GFDL. Modified: converted to markdown, study notes added. Offline snapshot of 9 February 2025.