std::is_constructible, std::is_trivially_constructible, std::is_nothrow_constructible
| Definido no cabeçalho <type_traits>
|
||
template< class T, class... Args > struct is_constructible; |
(1) | (desde C++11) |
template< class T, class... Args > struct is_trivially_constructible; |
(2) | (desde C++11) |
template< class T, class... Args > struct is_nothrow_constructible; |
(3) | (desde C++11) |
1) Se T é um objeto ou tipo de referência e a definição de variável T obj(std::declval<Args>()...); é bem-formada, então provê o membro constante value igual a true. Para todos os outros casos, value é false.
Para o fim dessa validação, a definição da variável nunca é interpretada como a definição de uma função, e o uso de std::declval não é considerado uma "odr-use". Validações de acesso são feitas como se viessem de um contexto não relacionado a T nem a quaisquer tipos de Args. Apenas a validade do contexto imediato da definição da variável é considerada.
2) o mesmo que 1), mas a definição da variável não chama nenhuma operação que não seja trivial. Para os fins dessa validação, a chamada a std::declval é considerada trivial.
3) o mesmo que 1), mas a definição da variável é noexcept.
T e todos os tipos na lista de parâmetros Args devem cada um ser um tipo completo, (possivelmente qualificado como cv) void, ou uma sequência de tamanho indefinido. Caso contrário, o comportamento é indefinido.
Modelos de variáveis auxiliares
<tbody> </tbody> template< class T, class... Args > inline constexpr bool is_constructible_v = is_constructible<T, Args...>::value; |
||
template< class T, class... Args > inline constexpr bool is_trivially_constructible_v = is_trivially_constructible<T, Args...>::value; |
||
template< class T, class... Args > inline constexpr bool is_nothrow_constructible_v = is_nothrow_constructible<T, Args...>::value; |
||
Herdado de std::integral_constant
Member constants
value [estática] |
true se T pode ser construído a partir de Args... , false contrário Original: true if T pode ser construído a partir de Args... , 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>
|
Notas
Em várias implementações, is_nothrow_constructible também verifica se o destruidor lança exceções, por ser definido como noexcept(T(arg)). O mesmo se aplica a is_trivially_constructible, que, nessas implementações, também requer que o destruidor seja trivial: GCC bug 51452 LWG issue 2116.
Exemplo
#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() {
std::cout << "Foo is ...\n" << std::boolalpha
<< "\tTrivially-constructible from const Foo&? "
<< std::is_trivially_constructible<Foo, const Foo&>::value << '\n'
<< "\tTrivially-constructible from int? "
<< std::is_trivially_constructible<Foo, int>::value << '\n'
<< "\tConstructible from int? "
<< std::is_constructible<Foo, int>::value << '\n'
<< "\tNothrow-constructible from int? "
<< std::is_nothrow_constructible<Foo, int>::value << '\n'
<< "\tNothrow-constructible from int and double? "
<< std::is_nothrow_constructible<Foo, int, double>::value << '\n';
}
Saída:
Foo is ...
Trivially-constructible from const Foo&? true
Trivially-constructible from int? false
Constructible from int? true
Nothrow-constructible from int? false
Nothrow-constructible from int and double? true
Veja também
verifica se um tipo tem um construtor padrão Original: checks if a type has a default 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 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) |