C++03이랑 C++11에서 컴파일되는 코드가 다른 결과를 낼 수 있나요?
조회수 2183회
질문이랑 똑같습니다.
C++03
이랑 C++11
에서 컴파일되는 코드가 다른 결과를 낼 수 있나요?
한쪽에서 실행이 안 되는 경우 말구요. 둘 다 실행은 되는데 결과가 다른 경우가 있을까요?
1 답변
-
물론 가능합니다. 자주 있는 일은 아니지만 그런 예시를 보여드릴게요
- String literal에서
#define u8 "abc" const char* s = u8"def";
의
s
는C++03
:"abcdef"
C++11
:"def"
- 0으로 Type 변환할 때
void f(void *); // #1 void f(...); // #2 template<int N> void g() { f(0*N); }
f(0*N)
에서C++03
: #1이 호출됨C++11
: #2이 호출됨
- int형 나누기(/), 나머지(%)의 결과를 올림 할 때
C++03
:0
또는negative inifinity
로 올림 할 수 있음C++11
: 오직0
만 허용
int i = (-1) / 2;
//C++03
에서는-1
,C++11
에서는0
템플릿에서 >>, > >(중간에 공백)를 처리할 때
- c++03에서
>>
은right-shift
로 해석될 수 있음
template< unsigned len > unsigned int fun(unsigned int x); typedef unsigned int (*fun_t)(unsigned int); template< fun_t f > unsigned int fon(unsigned int x); void total(void) { // fon<fun<9> >(1) >> 2 //03,11 모두 사용 가능 unsigned int A = fon< fun< 9 > >(1) >>(2); // C++03에서는 fon<fun<4> >(2) // C++11에서는 컴파일에러 unsigned int B = fon< fun< 9 >>(1) > >(2); }
- new연산자가 throw하는 exeption
c++03
:new
가 실패하면bad_alloc
throwc++11
:new
가 실패하면exception
throw
struct foo { void *operator new(size_t x){ throw std::exception(); } } try { foo *f = new foo(); catch (std::bad_alloc &) { // c++03 code } catch (std::exception &) { // c++11 code }
- 유저가 정의한 소멸자의 exception
C++03
: 아무exception
이나 throw 할 수 있음.C++11
:exception
을 throw 할 수 없음.exception
이 발생한 경우 std::terminate를 call (기본으로noexcept
이 설정되어 있기 때문)
struct A { ~A() { throw "foo"; } // C++11에서는 std::terminate을 call }; //... try { A a; } catch(...) { // C++03에서는 exception을 catch }
- container의 size() 함수의 수행 시간
C++03
: 최악의 경우 O(n)C++11
: O(1)
std::list<double> list; // ... size_t s = list.size(); // C++11에서 더 빨라짐!
- C++11부턴
std::ios_base::failure
이std::exception
에서 분리됨
댓글 입력