Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Possibly learning old C++ standard

Tags:

c++

c++17

I am interested in learning C++, and I found a book that seems to be the right fit for me. However, even though the book is quite new, some readers criticized that an earlier version of the book did not really dive into the new C++ standards (such as C++11, C++14, C++17) and rather used the "old C++ standard" (i.e., pre C++11).

While I checked if the newer version covered the new aspects of C++11 etc. (and it seemed like it covered quite a few), I am uncertain if possibly learning a pre C++11 standard would be inefficient? It seems like the new version does covers the new C++ standards (C++11 and upwards), but let's assume it does not. Would the knowledge I gain be outdated?

Again, I am new to C++, so I am not sure if the newer C++ standards completely changed the language, possibly removing/changing (integral) parts of the language which I would learn with this (possibly outdated) book. I just want to avoid learning (integral) parts of the language which are not used anymore or which are used differently nowadays (with the newer standards). However, if all the new C++ standards did was adding new features (but not removing/changing old features), I don't think learning from the book would be an issue since I could simply learn the newer features afterwards.

So in what ways did the newer C++ standards change the language, and would you say learning from a possibly outdated book is an issue?

like image 723
IT'S-ON Avatar asked Jan 18 '21 00:01

IT'S-ON


People also ask

Is it hard to learn C++ after C?

Switching from C to C++ can be both easy, as there are many similarities between the two languages, and hard, as there are many differences that require forgetting what you know and habits that you may have developed from programming in C.

Is it better to learn C or C++ first?

There is no need to learn C before learning C++. They are different languages. It is a common misconception that C++ is in some way dependent on C and not a fully specified language on its own. Just because C++ shares a lot of the same syntax and a lot of the same semantics, does not mean you need to learn C first.

Can I learn C language in 10 days?

Likewise, the educational program isn't excessively intricate or tedious to follow, as all you require is to experience a few subjects every day and you'll cover the whole schedule in basically 10 days. Along these lines, plunge into the C language world and improve your programming abilities for new job openings!

What is the best version of C++ to use?

We recommend choosing the latest standard “ISO C++ Latest (/std:c++latest)”, which as of the time of writing is the setting for C++20 support. For more information on Visual Studio language standard settings, Microsoft has a Visual Studio language standard reference document.

Do I need to learn C before learning C++?

There is no need to learn C before learning C++. They are different languages. It is a common misconception that C++ is in some way dependent on C and not a fully specified language on its own. Just because C++ shares a lot of the same syntax and a lot of the same semantics, does not mean you need to learn C first.

What is the C standard library?

The C Standard Library is a set of C built-in functions, constants and header files like <stdio.h>, <stdlib.h>, <math.h>, etc. This library will work as a reference manual for C programmers. The C Standard Library is a reference for C programmers to help them in their projects related to system programming.

What is the C standard?

The answer to all such questions is C standard. In all such cases, we need to see what C standard says about such programs. What is C standard? The latest C standard is ISO/IEC 9899:2011, also known as C11 as the final draft was published in 2011. Before C11, there was C99. The C11 final draft is available here.

What is the latest version of C standard?

What is C standard? The latest C standard is ISO/IEC 9899:2011, also known as C11 as the final draft was published in 2011. Before C11, there was C99. The C11 final draft is available here. See this for a complete history of C standards. Can we know the behavior of all programs from C standard?


1 Answers

Some code have changed in C++11, main changes, IMO, are:

  • way to "return" big object:

    • Pre-C++11, it has been recommended to use output parameter to avoid copy:
    void MakeVector(std::vector<int>& v) {
        v.clear();
        // fill v;
    }
    
    • Since C++11, no longer required for movable types, and return by value is the way to go:
    std::vector<int> MakeVector() {
        std::vector<int> v;
        // fill v;
        return v;
    }
    
  • way to iterate container:

    • Pre-C++11, it would be a mix between index and iterator way:
    void Foo(std::vector<int>& v) {
        for (std::vector<int>::iterator it = v.begin(); it != v.end(); ++it) { /* *it ..*/ }
    
        for (std::size_t i = 0; i != v.size(); ++i) { /* v[i] ..*/ }
    }
    
    • Since C++11, it is shorter (and optimal (end() is computed only once)):
    void Foo(std::vector<int>& v) {
        for (int& e : v) { /* ..*/ }
    }
    
  • way to write types:

    • Pre-C++11, type should be explicitly written:
    void Foo(std::vector<int>& v) {
        std::vector<int>::iterator it = std::find(v.begin(), v.end(), 42);
        // ...
    }
    
    • Since C++11, type can be deduced:
    void Foo(std::vector<int>& v) {
        auto it = std::find(v.begin(), v.end(), 42);
        // ...
    }
    
  • way to create predicate:

    • Pre-C++11, predicate should be done outside of the function as function or class:
    bool less_than_42(int i) { return i < 42; }
    
    struct less_than_x {
        less_than_x(int x) : x(x) {}
        bool operator()(int i) const { return i < x; }
        int x;
    };
    
    void Foo(std::vector<int>& v, int x) {
        std::vector<int>::iterator it1 = std::find_if(v.begin(), v.end(), less_than_42);
        std::vector<int>::iterator it2 = std::find_if(v.begin(), v.end(), less_than_x(x));
    
        // ...
    }
    
    • Since C++11, lambda simplify stuff (dedicated function might still be useful to avoid to duplicate lambda though):
    void Foo(std::vector<int>& v, int x) {
        auto it1 = std::find_if(v.begin(), v.end(), [](int e){ return e < 42; });
        auto it2 = std::find_if(v.begin(), v.end(), [x](int e){ return e < x; });
        // ...
    }
    

There are other changes, but which less invalidate C++03 way to code.

like image 108
Jarod42 Avatar answered Oct 18 '22 02:10

Jarod42