constexpr

2256 단어 constexprcppconstexpr

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가 포기할 수도 있다.

좋은 웹페이지 즐겨찾기