ETC/Talk

Stack Overflow를 돌아다니다가 C++ Style Cast (Reinterpret_cast) vs C-Style cast 라는 주제를 보았다.

VallistA2015. 4. 2. 00:38

제목과 같이 흥미로운 소재를 가진 (사실상 Reinterpret_cast가 C++ Style cast를 대변하는건 아니지만) 글을 보았는데 정말 흥미롭기 그지없었다.


역시 요즘 글들은 전부 Unity등의 엔진으로 도배되어 있어 별 흥미를 끌지 못하다가 오랜만에 재미있는 것을 보게되었다.


먼저 링크는..

http://stackoverflow.com/questions/7831696/reinterpret-cast-vs-c-style-cast


질문은 단순히 

hear that reinterpret_cast is implementation defined i don't know what does that really mean. Can you provide an example how can it go wrong? If it does go wrong, then is it better to use C-Style cast?


나는 reinterpret_cast 구현이 의미 없다고 생각하고 쓰는 이유를 모르겠고 정의가 왜 되어있는지 정말 알 수가 없다.
블라블라블라~ 어쨋건 C-Style Cast가 낫냐 C++ Style Cast가 낫냐 이말이올시다.


그에 대해서 추천을 11개나 (무려) 받은 답변이 있었다.


The C-style cast isn't better.

It simply tries the various C++-style casts in order, until it finds one that works. that means that when it acts like a reinterpret_cast, it has the exact same problems as a reinterpret_cast. But in addition, it has these problems:

  • it can do many different things, and it's not always clear from reading the code which type of cast will be invoked (it might behave like a reinterpret_cast, a const_cast or a static_cast, and those do very different things),
  • similarly, changing the surrounding code might change the meaning of the cast,
  • it's hard to search for (reinterpret_cast is easy to find, which is good, because casts are ugly and should be paid attention to when used. But a C-style cast, as in (int)42.0, is much harder to find by searching)

To answer the other part of your question, yes, reinterpret_cast is implementation-defined. This means that when you use it to convert from, say, an int* to a float*, then you have no guarantee that the resulting pointer will point to the same address. That part is implementation-defined. But if you take the resulting float* and reinterpret_cast it back into an int*, then you will get the original pointer. That part is guaranteed.

But again, remember that this is true whether you use reinterpret_cast or a C-style cast:


C++ Style cast 가 더 낫다.

그건 하나를 캐스트하기위해 다양한 C++ 스타일을 위해 캐스팅을 시도한다. 즉 reinterpret_cast 같은 역할을 할 때, reinterpret_cast 같은 동일한 문제를 가지고 있다는 것을 의미한다. 이러한 문제가 있다.

- 여러가지 일을 할 수 있고, 항상 캐스트의 종류가 호출되는 코드가 분명하지 않음. (그것은 확실한 일이 아닌 다른 일들을 reinterpret_cast, const_Cast 또는 static_cast 처럼 작동 할 수도 있음) -> 즉 뭐로 캐스팅 되는지 확실하지 않음

- 마찬가지로 캐스트의 의미가 변경 될 경우 주변 코드 변경의 검색이 어려움. (캐스트 치환하고 사용 할 때 주의를 기울여야 함. 그러나 (int)42.0 C 스타일 캐스트 같은 것에서는, 뭐로 될 지 모르기 때문에 reinterpret_cast이 더 좋다.)

다른 부분의 질문에 대답하자면 그것에 대한 대답은 '예' 다.

reinterpret_cast는 implementation-defined 다. (구현에 대한 정의)


이렇게 float* 에 있는 int* 을 변환하는 데 사용 할 때, 당신은 결과 포인터가 같은 주소를 가리킬 것이라는 보장이 없다는 것을 의미한다. 하지만 reinterpret_cast의 경우에는 float*를 int* 캐스트 하면 원래의 포인터를 얻을 것이다 라는 부분이 보장된다.


=> 정리를 하자면, C스타일의 캐스트는 C++ 스타일 캐스트에 맞는 것을 검색을 해서 그거에 맞는 캐스트를 찾아서 사용한다 이말인데, ㅇ이렇게 되면 C스타일 캐스트는 캐스트 되기전 탐색 시간까지 있으므로 훨씬 느리다는 것이고, 만약 reinterpret_cast 가 가지고 있는 문제가 있는데 그것이 문제가 될 때 reinterpret_cast로 캐스팅 되면 문제를 공유한다는 것이고 중요한 것은 우리가 어떤 캐스트건 reinterpret_cast 이건간 뭐든지 설정을 할 수 없다는 것이다.


즉 컴파일러가 그렇게 만들어져 있기때문에 우린 구현체에 대한 정의 (implementation-defined) 에 따라야 한다는 것임.

예제 :



댓글

VallistA

병특이 끝나서 게임에서 웹으로 스위칭한 프로그래머.
프로그래밍 정보등을 공유합니다.
현재는 이 블로그를 운영하지 않습니다.
vallista.kr 로 와주시면 감사하겠습니다!

자고 싶습니다. ㅠㅠ

Github      :: 링크

궁금한점 문의 주시면 답변드리도록 하겠습니다

VISITED

Today :

Total :

SNS

  • 페이스북아이콘
  • 카카오톡아이콘
  • 트위터아이콘

Lately Post

Lately Comment