constexpr

headkio·2020년 9월 19일
0

C++

목록 보기
30/35
post-thumbnail

constexpr ?

컴파일중 값으로 변경할 수 있으면 런타임이 아닌 바로 값으로 변경해 놓는다.
변경이 불가능하면 런타임시 함수 수행.

함수가 아닌 변수일 경우, 값으로 변경이 불가능하면 에러가 발생한다.

constexpr을 사용하지 않아도 자발적으로 값으로 변경해 놓는 컴파일러도 있다.

constexpr은 컴파일시 평가해 달라는 개발자의 의도를 보여주는 방법이다.

int value = 3;

int result = Factorial(3); // OK 
int result = Factorial(value); // OK 
constexpr int result = Factorial(value); // Compile Error !
constexpr int result = Factorial(3); // OK 
constexpr int result = Factorial(300); 
  // Compile Error ! -> 너무 오래 걸리면 Compiler가 포기할 수도 있다.
profile
돌아서서 잊지말고, 잘 적어 놓자

0개의 댓글