본문으로 건너뛰기 C++20 Designated Initializers 완벽 가이드 | 명확한 구조체 초기화

C++20 Designated Initializers 완벽 가이드 | 명확한 구조체 초기화

C++20 Designated Initializers 완벽 가이드 | 명확한 구조체 초기화

이 글의 핵심

C++20 Designated Initializers : 명확한 구조체 초기화. Designated Initializers란? 왜 필요한가와 기본 문법를 축으로 문법·패턴·주의점을 예제와 함께 설명합니다.

들어가며

구조체 멤버가 많을 때 이런 코드를 본 적 있나요?

Config cfg = {"localhost", 8080, true, 30, 100};

문제: 8080이 port인가요, 아니면 timeout인가요? 30은 뭔가요?

C++20 Designated Initializers를 사용하면 멤버 이름을 명시해서 이 혼란을 없앨 수 있습니다.

명확해진 코드

struct Config {
    std::string host;
    int port;
    bool ssl;
    int timeout;
    int max_connections;
};

// ✅ C++20: 멤버 이름을 명시
Config cfg = {
    .host = "localhost",
    .port = 8080,
    .ssl = true,
    .timeout = 30,
    .max_connections = 100
};

이제 각 값이 무엇인지 한눈에 알 수 있습니다!

장점

기존 초기화Designated Initializers
{"val1", val2, val3}.name1 = val1, .name2 = val2
순서 외우기이름으로 명확히
실수하기 쉬움실수 방지
순서 바뀌면 버그순서 무관 (C++에선 순서 권장)

1. 기본 문법

기본 사용

struct Point {
    int x;
    int y;
};
int main() {
    // C++20 Designated Initializers
    Point p1 = {.x = 10, .y = 20};
    
    // 중괄호 생략 가능
    Point p2{.x = 30, .y = 40};
    
    std::cout << p1.x << ", " << p1.y << '\n';  // 10, 20
}

일반 초기화와 비교

struct Person {
    std::string name;
    int age;
    std::string city;
};
int main() {
    // 일반 초기화 (순서 중요)
    Person p1 = {"Alice", 30, "Seoul"};
    
    // Designated Initializers (멤버 이름 명시)
    Person p2 = {
        .name = "Bob",
        .age = 25,
        .city = "Busan"
    };
}

2. 순서 규칙

선언 순서 유지 필수

C++20에서는 Designated Initializers가 선언 순서를 따라야 합니다 (C와 다름).

struct Data {
    int a;
    int b;
    int c;
};
int main() {
    // ✅ 선언 순서대로
    Data d1 = {.a = 1, .b = 2, .c = 3};  // OK
    
    // ❌ 순서 위반
    // Data d2 = {.b = 2, .a = 1, .c = 3};  // Error in C++20
    
    // ✅ 건너뛰기는 OK
    Data d3 = {.a = 1, .c = 3};  // b = 0 (기본값)
}

C vs C++20 차이

항목CC++20
순서자유선언 순서 필수
혼용가능불가
배열자유 인덱스순차만
// C에서는 OK
struct Data { int a, b, c; };
Data d = {.c = 3, .a = 1, .b = 2};  // C: OK, C++20: Error

3. 중첩 구조체

중첩 초기화

struct Address {
    std::string city;
    std::string street;
    int zipcode;
};
struct Employee {
    std::string name;
    int id;
    Address address;
};
int main() {
    Employee emp = {
        .name = "Alice",
        .id = 100,
        .address = {
            .city = "Seoul",
            .street = "Gangnam",
            .zipcode = 12345
        }
    };
    
    std::cout << emp.address.city << '\n';  // Seoul
}

깊은 중첩

struct Location {
    double lat;
    double lon;
};
struct Address {
    std::string street;
    Location location;
};
struct Person {
    std::string name;
    Address address;
};
int main() {
    Person p = {
        .name = "Bob",
        .address = {
            .street = "Main St",
            .location = {
                .lat = 37.5,
                .lon = 127.0
            }
        }
    };
}

4. 기본값과 부분 초기화

기본값 제공

struct Settings {
    int width = 800;
    int height = 600;
    bool fullscreen = false;
    int fps = 60;
};
int main() {
    // 일부만 지정, 나머지는 기본값
    Settings s1 = {
        .width = 1920,
        .height = 1080
    };
    // fullscreen = false, fps = 60
    
    Settings s2 = {
        .fullscreen = true
    };
    // width = 800, height = 600, fps = 60
}

빈 초기화

struct Point {
    int x;
    int y;
};
int main() {
    Point p = {};  // x = 0, y = 0
}

5. 자주 발생하는 문제와 해결법

문제 1: 순서 위반

증상: error: designator order for field does not match declaration order.

struct Data {
    int a;
    int b;
    int c;
};
int main() {
    // ❌ 순서 위반
    // Data d = {.b = 2, .a = 1};  // Error
    
    // ✅ 선언 순서
    Data d = {.a = 1, .b = 2};  // OK
}

문제 2: 일반 초기화와 혼용

증상: error: cannot mix designated and non-designated initializers.

struct Point {
    int x;
    int y;
    int z;
};
int main() {
    // ❌ 혼용 불가
    // Point p = {10, .y = 20, .z = 30};  // Error
    
    // ✅ Designated만
    Point p1 = {.x = 10, .y = 20, .z = 30};  // OK
    
    // ✅ 일반만
    Point p2 = {10, 20, 30};  // OK
}

문제 3: 비 Aggregate 타입

증상: error: designated initializers cannot be used with a non-aggregate type. 원인: 생성자가 있거나, private 멤버가 있는 클래스는 Aggregate가 아닙니다.

// ❌ 생성자 있음
class MyClass {
public:
    MyClass(int x) : value(x) {}
    int value;
};
// MyClass obj = {.value = 10};  // Error
// ✅ Aggregate (생성자 없음)
struct MyStruct {
    int value;
};
MyStruct obj = {.value = 10};  // OK

6. 프로덕션 패턴

패턴 1: 설정 구조체

struct ServerConfig {
    std::string host = "0.0.0.0";
    int port = 8080;
    bool ssl = false;
    int timeout_ms = 30000;
    int max_connections = 1000;
    std::string log_level = "info";
};
ServerConfig load_config() {
    // 기본값 + 일부 오버라이드
    return ServerConfig{
        .host = "localhost",
        .port = 9000,
        .ssl = true
        // 나머지는 기본값
    };
}

패턴 2: 빌더 패턴 대체

// Before: 빌더 패턴
class ConfigBuilder {
public:
    ConfigBuilder& setHost(std::string h) { host = h; return *this; }
    ConfigBuilder& setPort(int p) { port = p; return *this; }
    Config build() { return Config{host, port, ...}; }
private:
    std::string host;
    int port;
};
// After: Designated Initializers (더 간단)
struct Config {
    std::string host = "localhost";
    int port = 8080;
    bool ssl = false;
};
Config cfg = {
    .host = "example.com",
    .port = 443,
    .ssl = true
};

패턴 3: 테스트 데이터

struct TestCase {
    std::string name;
    int input;
    int expected;
};
std::vector<TestCase> tests = {
    {.name = "zero", .input = 0, .expected = 0},
    {.name = "positive", .input = 5, .expected = 25},
    {.name = "negative", .input = -3, .expected = 9}
};

7. 완전한 예제: HTTP 요청 설정

#include <string>
#include <map>
#include <chrono>
#include <iostream>
struct HttpHeaders {
    std::string content_type = "application/json";
    std::string authorization;
    std::string user_agent = "MyApp/1.0";
};
struct HttpRequest {
    std::string method = "GET";
    std::string url;
    HttpHeaders headers;
    std::string body;
    std::chrono::seconds timeout = std::chrono::seconds(30);
    bool follow_redirects = true;
    int max_redirects = 5;
};
void send_request(const HttpRequest& req) {
    std::cout << req.method << " " << req.url << '\n';
    std::cout << "Content-Type: " << req.headers.content_type << '\n';
    std::cout << "Timeout: " << req.timeout.count() << "s\n";
}
int main() {
    // GET 요청 (기본값 활용)
    HttpRequest get_req = {
        .url = "https://api.example.com/users"
    };
    send_request(get_req);
    
    // POST 요청 (일부 오버라이드)
    HttpRequest post_req = {
        .method = "POST",
        .url = "https://api.example.com/users",
        .headers = {
            .content_type = "application/json",
            .authorization = "Bearer token123"
        },
        .body = R"({"name":"Alice","age":30})",
        .timeout = std::chrono::seconds(60)
    };
    send_request(post_req);
}

정리

개념설명
Designated Initializers멤버 이름으로 초기화
문법{.member = value}
순서선언 순서 유지 필수 (C++20)
혼용일반 초기화와 혼용 불가
용도설정, 테스트 데이터, API 파라미터
Designated Initializers는 코드 가독성을 높이며, 구조체 초기화를 명확하게 만듭니다.

FAQ

Q1: C와 C++20 차이는?

A: C는 순서가 자유롭고 건너뛰기가 가능하지만, C++20은 선언 순서를 따라야 하며, 일반 초기화와 혼용할 수 없습니다.

Q2: 순서를 지키지 않으면?

A: 컴파일 에러가 납니다. 멤버 선언 순서대로 지정해야 합니다.

Q3: 일부 멤버만 지정하면?

A: 나머지 멤버는 기본값 또는 0으로 초기화됩니다. 기본값을 제공하면 명시하지 않은 멤버는 기본값을 사용합니다.

Q4: 클래스에서도 사용 가능한가요?

A: Aggregate 타입에서만 사용 가능합니다. 생성자가 있거나 private 멤버가 있는 클래스는 불가능합니다.

Q5: 배열은?

A: C++20에서는 배열 Designated Initializers가 제한적입니다. 순차 인덱스만 가능하며, 건너뛰기는 불가능합니다.

Q6: Designated Initializers 학습 리소스는?

A:


같이 보면 좋은 글 (내부 링크)

이 주제와 연결되는 다른 글입니다.

관련 글


이 글에서 다루는 키워드 (관련 검색어)

C++, initializer, cpp20, struct, syntax, aggregate 등으로 검색하시면 이 글이 도움이 됩니다.