Sections[lang]

Language

The core language as it is written now, from values and moves to lambdas and modules, for developers who learned C++ once and need the current answer.

Clause
1 of 8
Answers
37, newest first
Page
1 of 4
Written for
C++11, C++17
Written by

Much of what you write on every line has been reworked since C++03: how values are passed, moved and returned, what the compiler writes for a class, what auto deduces. This section covers those rules for the standard you compile with. Start with what std::move and && really do, then the special members C++ writes for you and when to write none of them. Since C++11 a throwing destructor calls std::terminate; can a C++ destructor throw covers what noexcept(false) changes.

In this section

  • Value semantics and passing arguments
  • Move semantics and rvalue references
  • Copy elision and returning values
  • Special member functions and the rule of zero
  • Lambdas and closures
  • Type deduction with auto and decltype
  • const, constexpr and consteval
  • Exceptions, noexcept and error handling
  • References, pointers and lifetimes
  • Classes, inheritance and polymorphism
  • Initialization rules
  • Modules and headers

Answers in Language 37, newest first

  1. assert in C++: how assert and static_assert work

    assert in C++ explained: what a failed assert prints, how to add a message, how NDEBUG turns it off, and how static_assert checks at compile time.

    §1.76 min readC++17

    written for C++17

  2. bool in C++: true, false and how to use them

    bool in C++ explained: true and false, what produces a bool, why cout prints 1 or 0, what converts to bool, bool functions and the common mistakes.

    §1.87 min readC++17

    written for C++17

  3. const in C++: variables, pointers, references, methods

    What const means in C++ on a variable, a pointer, a reference and a member function, with the error the compiler gives for each, and const vs constexpr.

    §1.97 min readC++17

    written for C++17

  4. constexpr in C++: variables, functions and static constexpr

    What constexpr means in C++: constexpr variables and functions, constexpr vs const, static constexpr in a class and inline constexpr in a header.

    §1.107 min readC++17

    written for C++17

  5. C++ array: declare, initialize and get the length

    How to declare and initialize a C++ array, loop over it, get its length with std::size, why sizeof fails in a function, and std::array vs vector.

    §1.117 min readC++17

    written for C++17

  6. C++ class: how to define one and create objects

    C++ class explained with one Account example: private data, public functions, objects, a constructor, const member functions, header files, class vs struct.

    §1.127 min readC++17

    written for C++17

  7. C++ constructor: default, parameterized, copy and move

    What a C++ constructor is and how to write one: default, parameterized, copy and move constructors, the initializer list, explicit and structs.

    §1.138 min readC++11

    written for C++11

  8. continue in C++: skip to the next loop iteration

    continue in C++: where each loop goes next, why a while loop with continue never ends, continue vs break, nested loops and switch.

    §1.147 min readC++17

    written for C++17

  9. Data types in C++: int, double, char, bool and more

    The C++ data types in one table: what int, double, char and bool hold, their sizes and ranges, how to check them with sizeof, and which one to use.

    §1.157 min readC++17

    written for C++17

  10. C++ do while loop: syntax, examples and do while vs while

    The C++ do while loop: its syntax and semicolon, do while vs while, a menu and an input check that survive a letter, and how break and continue behave.

    §1.167 min readC++17

    written for C++17