1

와 배열 구성원 초기화 :C++ -이 코드로 집계 초기화

struct Structure { 
    int a; 
    char b[4]; 
}; 

void function() { 
    int a = 3; 
    char b[] = {'a', 'b', 'c', 'd'}; 
} 

내가 집계 초기화를 사용 a의 값으로 Structureb를 초기화 할 수 있습니까?
나는 나에게 오류를 제공하는 그러나, Structure{a, b}을 시도 cannot initialize an array element of type 'char' with an lvalue of type 'char [4]'

+0

당신은 [] B 모두 '문자를 변경하는 경우' 'std :: array '에 의해, 네. [데모] (http://coliru.stacked-crooked.com/a/8ac7cfe90b9a75e0) – Jarod42

답변

0
struct S { 
    int a; 
    char b[4]; 
}; 

int main() { 
    S s = { 1, {2,3,4,5} }; 
} 

편집 : 그냥 질문을 다시 읽기 - 아니, 당신은 할 수 없습니다. 다른 배열로 배열을 초기화 할 수 없습니다.

struct Structure { 
    int a; 
    char b[4]; 
}; 

template< typename... I, typename... C> 
void function(I... a, C... b) { 
    Structure s = { a..., b... }; // <- here -> a = 1 and b = 'a','b','c','d' 
    std::cout << s.a << '\n'; 
    for(char chr : s.b) std::cout << chr << ' '; 
} 

int main(){ 
    function(1, 'a','b','c','d'); 
} 

출력은 : 당신은 내가 이런 식으로, 당신이 할 수있는 생각 매개 변수 팩 - 확장에 익숙하다면

0

1 
a b c d