Can the code compiled in C++03 and C++11 produce different results?

Asked 2 years ago, Updated 2 years ago, 55 views

It's the same as the question. Can the code compiled by C++03 and C++11 produce different results? Not if it doesn't work on one side. Is there a case where both are executed, but the results are different?

c++ c++11 language-lawyer c++03

2022-09-22 10:49

1 Answers

Of course it's possible. It doesn't happen often, but I'll show you an example like that.

#define u8 "abc"
const char* s = u8"def";

s in is

void f(void *); // #1
void f(...); // #2
template<int N> void g() {
    f(0*N);
}

In f(0*N)

inti=(-1) /2; // C++03 -1 for C++11 and 0 for C++11

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) {
    // fun<fun<9> >(1) >> 2 //03,11 All available
    unsigned int A = fon< fun< 9 > >(1) >>(2);

    // In C++03, fun<fun<4> > (2)
    // Compilation error on C++11
    unsigned int B = fon< fun< 9 >>(1) > >(2);
}
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
}
struct A {
    ~A() {throw "foo";} // C++11 calls std::terminate
};
//...
try { 
    A a; 
} } catch(...) { 
    // In C++03, catch exception
} 
std::list<double> list;
// ...
size_ts = list.size(); // Faster at C++11!


2022-09-22 10:49

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.