std::is_default_constructible, std::is_trivially_default_constructible, std::is_nothrow_default_constructible
Definido no cabeçalho <type_traits>
|
||
template< class T > struct is_default_constructible; |
(1) | (desde C++11) |
template< class T > struct is_trivially_default_constructible; |
(2) | (desde C++11) |
template< class T > struct is_nothrow_default_constructible; |
(3) | (desde C++11) |
1) Se std::is_constructible<T>::value é true, provê o membro constante value
igual a true, senão, value
é false.
2) Se std::is_trivially_constructible<T>::value é true, provê o membro constante value
igual a true, senão, value
é false.
3) Se std::is_nothrow_constructible<T>::value é true, provê o membro constante value
igual a true, senão, value
é false.
T
deve ser um tipo completo, (possivelmente qualificado como cv) void, ou uma sequência de tamanho indefinido. Caso contrário, o comportamento é indefinido.
Índice |
[editar] Modelos de variáveis auxiliares
template< class T > inline constexpr bool is_default_constructible_v = is_default_constructible<T>::value; |
||
template< class T > inline constexpr bool is_trivially_default_constructible_v = is_trivially_default_constructible<T>::value; |
||
template< class T > inline constexpr bool is_nothrow_default_constructible_v = is_nothrow_default_constructible<T>::value; |
||
Herdado de std::integral_constant
Member constants
value [estática] |
true se T pode ser construído com um construtor padrão , false contrário Original: true if T pode ser construído com um construtor padrão , false otherwise The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (membro estático público constante) |
Member functions
operator bool |
converte o objeto em bool, retorna value Original: converts the object to bool, returns value The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (função pública membro) |
Member types
Tipo
Original: Type The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
Definition |
value_type
|
bool
|
type
|
std::integral_constant<bool, value> |
[editar] Possível implementação
template< class T> struct is_default_constructible : std::is_constructible<T> {}; template< class T> struct is_trivially_default_constructible : std::is_trivially_constructible<T> {}; template< class T> struct is_nothrow_default_constructible : std::is_nothrow_constructible<T> {}; |
[editar] Notas
Em várias implementações, is_nothrow_default_constructible
também verifica se o destruidor lança exceções, por ser definido como noexcept(T()). O mesmo se aplica a is_trivially_default_constructible
, que, nessas implementações, também requer que o destruidor seja trivial: GCC bug 51452 LWG issue 2116.
std::is_default_constructible<T> não testa se T x; é compilável; ele tenta uma inicialização direta com uma lista vazia de argumentos (veja std::is_constructible). Logo, std::is_default_constructible_v<const int> e std::is_default_constructible_v<const int[10]> são true.
[editar] Exemplo
#include <iostream> #include <type_traits> struct Ex1 { std::string str; // member has a non-trivial default ctor }; struct Ex2 { int n; Ex2() = default; // trivial and non-throwing }; int main() { std::cout << std::boolalpha << "Ex1 is default-constructible? " << std::is_default_constructible<Ex1>::value << '\n' << "Ex1 is trivially default-constructible? " << std::is_trivially_default_constructible<Ex1>::value << '\n' << "Ex2 is trivially default-constructible? " << std::is_trivially_default_constructible<Ex2>::value << '\n'; << "Ex2 is nothrow default-constructible? " << std::is_nothrow_default_constructible<Ex2>::value << '\n'; }
Saída:
Ex1 is default-constructible? true Ex1 is trivially default-constructible? false Ex2 is trivially default-constructible? true Ex2 is nothrow default-constructible? true
[editar] Veja também
(C++11) (C++11) (C++11) |
verifica se um tipo tem um construtor para argumentos específicos Original: checks if a type has a constructor for specific arguments The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (modelo de classe) |
(C++11) (C++11) (C++11) |
verifica se um tipo tem um construtor de cópia Original: checks if a type has a copy constructor The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (modelo de classe) |
(C++11) (C++11) (C++11) |
verifica se um tipo tem um construtor movimento Original: checks if a type has a move constructor The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (modelo de classe) |