std::convertible_to
From cppreference.com
C++
Feature test macros (C++20)
Concepts library (C++20)
Metaprogramming library (C++11)
Ranges library (C++20)
Filesystem library (C++17)
Concurrency support library (C++11)
Execution control library (C++26)
Concepts library
(C++20)
(C++20)
convertible_to
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)(C++20)
(C++20)(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
Exposition-only concepts
(C++20)
Defined in header
<concepts>
template< class From, class To >
(since C++20)
concept convertible_to =
std::is_convertible_v <From, To> &&
requires {
static_cast<To>(std::declval <From>());
The concept convertible_to<From, To> specifies that an expression of the same type and value category as those of std::declval <From>() can be implicitly and explicitly converted to the type To
, and the two forms of conversion produce equal results.
[edit] Semantic requirements
convertible_to<From, To> is modeled only if, given a function fun
of type std::add_rvalue_reference_t <From>() such that the expression fun() is equality-preserving,
- Either
-
To
is neither an object type nor a reference-to-object type, or - static_cast<To>(fun()) is equal to []() -> To { return fun(); }(), and
-
- One of the following is true:
- std::add_rvalue_reference_t <From> is not a reference-to-object type, or
- std::add_rvalue_reference_t <From> is an rvalue reference to a non-const-qualified type, and the resulting state of the object referenced by fun() is valid but unspecified after either expression above; or
- the object referred to by fun() is not modified by either expression above.
[edit] Equality preservation
Expressions declared in requires expressions of the standard library concepts are required to be equality-preserving (except where stated otherwise).
[edit] References
- C++23 standard (ISO/IEC 14882:2024):
- 18.4.4 Concept
convertible_to
[concept.convertible]
- 18.4.4 Concept
- C++20 standard (ISO/IEC 14882:2020):
- 18.4.4 Concept
convertible_to
[concept.convertible]
- 18.4.4 Concept