https://www.learncpp.com/cpp-tutorial/introduction-to-type-conversion-and-static_cast/
예를 들어 int에 담겨있는 데이터를 double로 옮길 때
혹은 그 반대
그런데 명시적으로 하지 않고
예를 들어 다음과 같은 경우
int x = 1;
double y = x;
실제로 컴파일도 되고 실행도 문제없이 된다
하지만 암묵적으로 진행되는 type conversion은
프로그래머가 의도했는지 알 수가 없다
따라서 이러한 스타일은 지양해야한다
따라서 다음과 같은 initialization 스타일을 추천했던 것이다
int main()
{
double d { 5 }; // okay: int to double is safe
int x { 5.5 }; // error: double to int not safe
return 0;
}
이것이 brace initialization의 주된 이유이다.
brace initialization은 data를 잃을 가능성이 있으면 error를 발생시킨다
implicit와 달리 compiler에게 명시적으로 알려주는 것이다
즉, 우리가 축소변환이더라도 우리가 의도한 type conversion이니 우리를 믿어라 이런 느낌
static_cast<new_type>(expression)