최대 1 분 소요

개요

  • 컴파일 타임에 계산이 불가능하면 컴파일 에러 발생


예제

  • 코드
    #include <compare>
    #include <iostream>

    using namespace std;

    consteval int func(int i) { return i; }

    int main() {
    	cout << func(1) << endl;

    	int i = 0;
    	cout << func(i) << endl;

    	return 0;
    }
  • 실행 결과
    test.cpp: In function ‘int main()’:
    test.cpp:12:22: error: the value of ‘i’ is not usable in a constant expression
       12 |         cout << func(i) << endl;
          |                      ^
    test.cpp:11:13: note: ‘int i’ is not const
       11 |         int i = 0;
          |             ^