about
10/29/2022
C++ Introduction
CppBites - Introduction
statically typed, native code, fast, complex language
Why C++ ?
Things you need to know:
- C++ is designed to support creation of user-defined value types that support construction and assignment of independent instance copies. Managed languages, like C# and Java, copy handles to instances on the heap, so an assignment in those languages results in two handles pointing to the same instance.
- The C++ language compiles to native code which runs directly in a new process. Code for managed languages run in a virtual machine hosted by a new process. That has consequences for both latency and throughput.
- C++ is complex, partly because it provides a lot of control over how platfrom resources are used, but also because the way you should write code for many operations depends on the context in which it is written.
TemplateMetaProgramming
- At design-time code is constructed that, when compiled, adds and modifies other code, which eventually runs when the program executes
- You will find a number of examples in CppStory_TemplateMetaprog.html
C++ highs
- C++ is very fast because it compiles to native code, does not use garbage collection, and much of its syntax and semantics are designed to operate as efficiently as is practical.
- It has strong static typing so syntax errors are evaluated at compile-time, not run-time as in dynamically typed languages.
- The language has a moderately large syntax footprint and a large collection of standard libraries. So you can find ways within the language to meet your implementation goals.
- It is widely used - a lot of currently running applications have been built with C++, there are many experienced developers with C++ expertise, and the tooling has been refined for years.
- Many third party libraries exist for math and scientific programming, graphics, games, databases, ...
- There are many sources of learning and reference material in the open literature. This site is one of the best: cppreference.com.
C++ lows
-
C++ is a very complex programming language. Much of its complexity is due to context dependencies.
Context Dependency Examples
-
The keyword "static" means three different things, depending on its context:
- As qualifier in a variable declaration in namespace or block scope, it means the variable is placed in static memory and has lifetime of its enclosing program. The variable is only initialized once, even if declaration occurs repeatedly in the program execution flow, and the variable is only visible in the scope in which it is defined.
- A static method can access only static member data, and is used with "::" instead of the "." operator.
- A static member variable has a single, possibly mutable, value that is shared by all instances of its class.
- Operations like construction and assignment may copy the source or may move ownership of the source's resources to the target. Which depends on whether the source is a temporary or a named entity or if the operation is in a context where return value optimization (RVO) applies.
- The rules for type deduction depend on whether the type is templated or not. And some of the rules are far from obvious.
- C++ provides strong, but not strict, static typing. The types of data can be modified at run-time through the use of casts; and many type conversions are made implicitly. That can make analysis of program operation more difficult and lead to some surprising results.
- The special methods you implement for a class - see below - depend on the types of its data members. And the compiler allows you, without warning, to fail to implement them when you should.
- and the list goes on ...
-
The keyword "static" means three different things, depending on its context:
- The language has a moderately large syntax footprint and a large collection of standard libraries. So you have to invest a lot of effort and years of experience to use most of it effectively.
- There are many paths to undefined behavior, e.g., reading and writing to memory the program does not own. Here's a demonstration.
A Taste of C++ Syntax
-
X(), X(const &X), X(&&X), and X(T)
are default, copy, move, and promotion constructors -
X& operator=(const &X) and X& operator=(&&X)
are copy and move assignment operators. -
T& operator[](size_t i) and T operator[](size_t i) const
are index operators for mutable and const instances. - ~X() is the class instance destructor.
STR Class
References
reference | comments |
---|---|
Wikipedia C++ | Survey of language origin, evolution, and properties |
C++ 11 - Herb Sutter video | Survey of important advancements |
Back to Basics - Herb Sutter video | Thoughtful advice about developing with C++ |
C++ 11 - Wikipedia | Survey of C++ 11 standard |
Writing good C++14 by default - Herb Sutter video | Writing relatively simple and clear C++ using features of C++14 |
C++ 14 - Wikipedia | Survey of C++ 14 standard |
C++ 17 the good and ugly - Nicolai Josuttis video | Informed opinions about C++ 17 |
C++ 17 features - Bryce Lelbach video | Survey of selected features |
C++ 17 - Wikipedia | Survey of C++ 17 standard |
Almost complete overview of C++20 - Marc Gregoire video | Survey of C++ 20 standard |
C++ 20 - Wikipedia | Survey of C++ 20 standard |