Skip to content

Latest commit

 

History

History
73 lines (48 loc) · 2.07 KB

LegacyCpp.org

File metadata and controls

73 lines (48 loc) · 2.07 KB

Legacy C++

Annex C compatibility

Invalid and Valid Code Between C++ 98/03 and C++ 11

  • The banning of narrowing conversions during aggregate initialization in C++11
    int a[] = { 1.0 }; // invalid C++ 11
        
  • >> on the right of template parameter is valid in C++11 but not in C++03
  • std::swap are defined in <utility> in C++11 but <algorithm> in C++98.
  • std::basic_ios has in C++11 explicit operator bool() const instead of a conversion that evaluates to bool.
  • string literals do not convert to char* in C++11.

Different Behaviors of the Same Code Under C++ 98/03 and C++ 11

  • Integer division always rounds to zero.
  • a destructor is now by default noexcept(true) unless specified otherwise, but C++98 allows exceptions.
  • Dependent function calls with internal linkage are considered in C++11
    static void f(int) { }
    void f(long) { }
    
    template<typename T>
    void g(T t) { f(t); }
    
    int main() { g(0); } // caslls f(int) in C++11 but f(long) in C++98
    
    // However, with instantiation, this still calls f(B)
    struct B { };
    struct A : B { };
    
    template<typename T>
    void g(T t) { f(t); }
    
    static void f(A) { }
    void f(B) { }
    
    int main() { A a; g(a); }
        
  • std::vector<T> a(50) resolves to a separate constructor that construct the elements in place in C++11 while in C++03 a default value of the second parameter is copied to each slot.
    • in case where a copy constructor taking a default value is different from the default constructor, this behavior is a breaking change.

Later Changes Since C++11

  • C++17 changed std::string::data to char* from const char* to allow easy in-place string data modification through a pointer.
  • Unnamed classes with a typedef name for linkage purposes (i.e. C++ code) can contain only

C-compatible constructs since C++20. Note this does not forbid the use inside extern "C" blocks or structs that contains only data.

// forbidden since C++20
typedef struct {
   void member_function();
} m_struct;