최대 1 분 소요

개요

  • 저장 기간이 정적 혹은 쓰레드인 변수가 컴파일 시점에 초기화됨을 보장
  • 비상수이므로 const 선언 가능


예제

  • 코드
    #include <iostream>

    using namespace std;

    constexpr int init1() { return 1; }
    const int init2() { return 2; }

    constinit int s1 = init1();
    // constinit int s2 = init2(); // error

    int main() {
    	cout << s1 << endl;

    	s1 = 7;

    	cout << s1 << endl;

    	return 0;
    }
  • 실행 결과
    1
    7