std::is_con...onstructible(3)
okay bestie, this page is called std::is_con...onstructible(3) and it lives in the C++ Standard Libary manual β yes, the title is truncated like that, and weβre about to make type traits make sense π
β¨
πΈ NAME πΈ
std::is_constructible, std::is_trivially_constructible, std::is_nothrow_constructible β the iconic trio, fr fr.
β¨ Synopsis β¨
Defined in header <type_traits>, and the signatures are giving template realness:
Defined in header <type_traits>
template< class T, class... Args > (1) (since C++11)
struct is_constructible;
template< class T, class... Args > (2) (since C++11)
struct is_trivially_constructible;
template< class T, class... Args > (3) (since C++11)
struct is_nothrow_constructible;
(1)β IfTis an object or reference type and the variable definitionT obj(std::declval<Args>()...);is well-formed, it provides the member constantvalueequal totrue. In all other cases,valueisfalse.
For the purposes of this check, the variable definition is never interpreted as a function declaration, and the use of std::declval is not considered an odr-use.
Access checks are performed as if from a context unrelated to T and any of the types in Args. Only the validity of the immediate context of the variable definition is considered.
(2)β Same as (1), but the variable definition does not call any operation that is not trivial. For the purposes of this check, the call tostd::declvalis considered trivial.(3)β Same as (1), but the variable definition isnoexcept.
π¨ undefined behavior alert, bestie β these are not a vibe:
- If
Tor any type in the parameter packArgsis not a complete type, (possibly cv-qualified)void, or an array of unknown bound, the behavior is undefined. - If an instantiation of a template above depends, directly or indirectly, on an incomplete type, and that instantiation could yield a different result if that type were hypothetically completed, the behavior is undefined.
- If the program adds specializations for any of the templates described on this page, the behavior is undefined.
π Member constants π
valueβtrueifTis constructible fromArgs...,falseotherwise.[static](public static member constant)
π Member functions π
operator boolβ converts the object tobool, returnsvalue(public member function)operator()β returnsvalue(C++14) (public member function)
π¦ Member types π¦
value_typeβbooltypeβstd::integral_constant<bool, value>
π Notes π
In many implementations, is_nothrow_constructible also checks if the destructor throws because it is effectively noexcept(T(arg)). Same applies to is_trivially_constructible, which, in these implementations, also requires that the destructor is trivial: GCC bug 51452, LWG issue 2116.
π Example π
// Run this code
#include <iostream>
#include <type_traits>
class Foo
{
int v1;
double v2;
public:
Foo(int n) : v1(n), v2() {}
Foo(int n, double f) noexcept : v1(n), v2(f) {}
};
int main()
{
auto is = [](bool o) { return (o ? "\t" "is " : "\t" "isn't "); };
std::cout << "Foo ...\n"
<< is(std::is_trivially_constructible_v<Foo, const Foo&>)
<< "Trivially-constructible from const Foo&\n"
<< is(std::is_trivially_constructible_v<Foo, int>)
<< "Trivially-constructible from int\n"
<< is(std::is_constructible_v<Foo, int>)
<< "Constructible from int\n"
<< is(std::is_nothrow_constructible_v<Foo, int>)
<< "Nothrow-constructible from int\n"
<< is(std::is_nothrow_constructible_v<Foo, int, double>)
<< "Nothrow-constructible from int and double\n";
}
Output β¨
Foo ...
is Trivially-constructible from const Foo&
isn't Trivially-constructible from int
is Constructible from int
isn't Nothrow-constructible from int
is Nothrow-constructible from int and double
π See also π
is_default_constructibleβ checks if a type has a default constructor (C++11) (class template)is_trivially_default_constructibleβ checks if a type has a default constructor (C++11) (class template)is_nothrow_default_constructibleβ checks if a type has a default constructor (C++11) (class template)is_copy_constructibleβ checks if a type has a copy constructor (C++11) (class template)is_trivially_copy_constructibleβ checks if a type has a copy constructor (C++11) (class template)is_nothrow_copy_constructibleβ checks if a type has a copy constructor (C++11) (class template)is_move_constructibleβ checks if a type can be constructed from an rvalue reference (C++11) (class template)is_trivially_move_constructibleβ checks if a type can be constructed from an rvalue reference (C++11) (class template)is_nothrow_move_constructibleβ checks if a type can be constructed from an rvalue reference (C++11) (class template)constructible_fromβ specifies that a variable of the type can be constructed from or bound to a set of argument types (C++20) (concept)
k bye bestie π this page is signed off from http://cppreference.com, version 2024.06.10, and the page std::is_con...onstructible(3) β go be constructible, or donβt, we donβt judge π«Ά periodt.
π¦ 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