aggregate initialization
Da cppreference.com
![]() | This page has been machine-translated from the English version of the wiki using Google Translate. The translation may contain errors and awkward wording. Hover over text to see the original version. You can help to fix errors and improve the translation. For instructions click here. |
Inicializa um agregado de braced-init-lista
Original:
Initializes an aggregate from braced-init-list
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
Índice |
[editar]Sintaxe
Tobject= { arg1, arg2, ...}; | (1) | ||||||||
Tobject { arg1, arg2, ...}; | (2) | (desde C++11) | |||||||
[editar]Explicação
Inicialização agregada é uma forma de lista de inicialização, que inicializa agregados'
Original:
Aggregate initialization is a form of lista de inicialização, which initializes aggregates
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
Um agregado é um objecto do tipo que é um dos seguintes procedimentos
Original:
An aggregate is an object of the type that is one of the following
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
- tipo de matrizOriginal:array typeThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - tipo de classe (geralmente, struct ou union), que possuiOriginal:class type (typically, struct or union), that hasThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
- não membros privados ou protegidosOriginal:no private or protected membersThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - não fornecido pelo usuário construtoresOriginal:no user-provided constructorsThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - sem classes baseOriginal:no base classesThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - sem funções de membro virtualOriginal:no virtual member functionsThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - não inicializadores cinta-ou-iguais para membros não-estáticosOriginal:no brace-or-equal initializers for non-static membersThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Os efeitos da inicialização agregada são:
Original:
The effects of aggregate initialization are:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
- Cada elemento da matriz ou não-estático membro da classe, por ordem de índice de matriz / aparência na definição da classe, é cópia inicializado da cláusula correspondente da lista de inicializador.Original:Each array element or non-static class member, in order of array subscript/appearance in the class definition, is cópia inicializado from the corresponding clause of the initializer list.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
- Se a cláusula é uma expressão inicializador, as conversões implícitas são permitidas, exceto (desde C++11) se eles estão estreitando (como em lista de inicialização).Original:If the initializer clause is an expression, implicit conversions are allowed, except (desde C++11) if they are narrowing (as in lista de inicialização).The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
- Se a cláusula é um inicializador aninhada preparou-init-lista, o membro da classe correspondente é em si um agregado: inicialização agregada é recursivo.Original:If the initializer clause is a nested braced-init-list, the corresponding class member is itself an aggregate: aggregate initialization is recursive.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
- Se o objeto é um array de tamanho desconhecido, ea lista de inicializador fornecido cinta-fechado tem
n
cláusulas, o tamanho da matriz én
Original:If the object is an array of unknown size, and the supplied brace-enclosed initializer list hasn
clauses, the size of the array isn
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
- Membros de dados estáticos e anônimos campos de bit são ignorados durante a inicialização do agregado.Original:Static data members and anonymous bit-fields are skipped during aggregate initialization.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
- Se o número de cláusulas inicializador excede o número de membros para inicializar, o programa é mal-formado (erro de compilador)Original:If the number of initializer clauses exceeds the number of members to initialize, the program is ill-formed (compiler error)The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
- Se o número de cláusulas inicializador é menor que o número de membros, os membros restantes são inicializados por listas vazias, que realiza valor de inicialização. Se um membro de um tipo de referência é um desses membros restantes, o programa é mal formado (referências não podem ser de valor inicializado)Original:If the number of initializer clauses is less than the number of members, the remaining members are initialized by empty lists, which performs valor de inicialização. If a member of a reference type is one of these remaining members, the program is ill-formed (references cannot be value-initialized)The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
- Se a inicialização agregado utiliza a forma com o sinal de igual (T a ={args..}), as cintas em torno das listas de inicializador aninhados podem ser omitidos (omitido), caso em que, como cláusulas inicializador quantas forem necessárias são usados para inicializar cada membro ou elemento do correspondente subaggregate, e as cláusulas inicializador subseqüentes são usados para inicializar os seguintes membros do objeto. No entanto, se o objeto tem um sub-conjunto sem membros (uma estrutura vazia, ou uma estrutura segurando apenas membros estáticos), elisão cinta não é permitido, e uma lista vazia
{}
aninhada deve ser usado.Original:If the aggregate initialization uses the form with the equal sign (T a ={args..}), the braces around the nested initializer lists may be elided (omitted), in which case, as many initializer clauses as necessary are used to initialize every member or element of the corresponding subaggregate, and the subsequent initializer clauses are used to initialize the following members of the object. However, if the object has a sub-aggregate without any members (an empty struct, or a struct holding only static members), brace elision is not allowed, and an empty nested list{}
must be used.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
- Quando um sindicato é inicializado pela inicialização agregado, apenas nos primeiros membros não-estáticos de dados é inicializado.Original:When a union is initialized by aggregate initialization, only its first non-static data members is initialized.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
[editar]Notas
Até C + +11, conversões de estreitamento foram autorizados em inicialização agregada, mas eles não são mais permitidas.
Original:
Until C++11, narrowing conversions were permitted in aggregate initialization, but they are no longer allowed.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
Até C + +11, inicialização agregada não poderia ser utilizado em uma lista de inicializador construtor devido a restrições de sintaxe.
Original:
Until C++11, aggregate initialization could not be used in a constructor initializer list due to syntax restrictions.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[editar]Exemplo
#include <string>#include <array>struct S {int x;struct Foo {int i;int j;int a[3];} b;}; union U {int a;constchar* b;};int main(){ S s1 ={1, {2, 3, {4, 5, 6}}}; S s2 ={1, 2, 3, 4, 5, 6};// same, but with brace elision S s3{1, {2, 3, {4, 5, 6}}};// same, using direct-list-initialization syntax// S s4{1, 2, 3, 4, 5, 6}; // error: brace-elision only allowed with equals sign int ar[]={1,2,3};// ar is int[3]// char cr[3] = {'a', 'b', 'c', 'd'}; // too many initializer clauseschar cr[3]={'a'};// array initialized as {'a', '\0', '\0'} int ar2d1[2][2]={{1, 2}, {3, 4}};// fully-braced 2D array: {1, 2}// {3, 4}int ar2d2[2][2]={1, 2, 3, 4};// brace elision: {1, 2}// {3, 4}int ar2d3[2][2]={{1}, {2}};// only first column: {1, 0}// {2, 0} std::array<int, 3> std_ar2{{1,2,3}};// std::array is an aggregatestd::array<int, 3> std_ar1 ={1, 2, 3};// brace-elision okay int ai[]={1, 2.0};// narrowing conversion from double to int:// error in C++11, okay in C++03 std::string ars[]={std::string("one"), // copy-initialization"two", // conversion, then copy-initialization{'t', 'h', 'r', 'e', 'e'}};// list-initialization U u1 ={1};// OK, first member of the union// U u2 = { 0, "asdf" }; // error: too many initializers for union// U u3 = { "asdf" }; // error: invalid conversion to int }