std::same_as(3)
π NAME π
Okay so welcome to std::same_as(3), bestie β straight outta the C++ Standard Libary (yes, that spelling is in the header, weβre not fixing it π).
std::same_as β std::same_as. Thatβs the whole name, no plot twist, periodt π
π Synopsis π
It lives in the header <concepts>, and itβs been here since C++20, iconic behavior:
Defined in header <concepts>
template< class T, class U > (since C++20)
concept same_as = /* see below */;
The concept same_as<T, U> is satisfied if and only if T and U denote the same type. Literally type equality, no cap β¨
Also important: std::same_as<T, U> subsumes std::same_as<U, T> and vice versa. Mutual subsumption, we love to see it π«Ά
π οΈ Possible implementation π οΈ
One possible implementation, and itβs giving helper-concept realness:
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 πΈ
Run this code, bestie, itβs a whole little printable-types moment π§:
// Run this code
#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:
Example: 3.14 : 42 : [a-Z]
π See also π
is_sameβ checks if two types are the same(C++11)(class template) π
Okay bye bestie π this page came from http://cppreference.com, dated 2024.06.10, and itβs std::same_as(3) β source/version/date all accounted for, mwah π
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms