C++ For C# Developers: Part 1 – Introduction
Outside of Unity, C# is rarely used as a game programming language. C++ is used heavily in Unreal, Cryengine, Lumberyard, and almost all proprietary game studio engines. This series is for Unity game programmers who know C# and want to broaden their skills so they can effectively write code for other engines, or even write C++ scripts for Unity. Today we’ll begin with an introduction to C++’s history, standard library, tools, community, and documentation. Read on to get started!
Table of Contents
- Part 1: Introduction
- Part 2: Primitive Types and Literals
- Part 3: Variables and Initialization
- Part 4: Functions
- Part 5: Build Model
- Part 6: Control Flow
- Part 7: Pointers, Arrays, and Strings
- Part 8: References
- Part 9: Enumerations
- Part 10: Struct Basics
- Part 11: Struct Functions
- Part 12: Constructors and Destructors
- Part 13: Initialization
- Part 14: Inheritance
- Part 15: Struct and Class Permissions
- Part 16: Struct and Class Wrapup
- Part 17: Namespaces
- Part 18: Exceptions
- Part 19: Dynamic Allocation
- Part 20: Implicit Type Conversion
- Part 21: Casting and RTTI
- Part 22: Lambdas
- Part 23: Compile-Time Programming
- Part 24: Preprocessor
- Part 25: Intro to Templates
- Part 26: Template Parameters
- Part 27: Template Deduction and Specialization
- Part 28: Variadic Templates
- Part 29: Template Constraints
- Part 30: Type Aliases
- Part 31: Destructuring and Attributes
- Part 32: Thread-Local Storage and Volatile
- Part 33: Alignment, Assembly, and Language Linkage
- Part 34: Fold Expressions and Elaborated Type Specifiers
- Part 35: Modules, The New Build Model
- Part 36: Coroutines
- Part 37: Missing Language Features
- Part 38: C Standard Library
- Part 39: Language Support Library
- Part 40: Utilities Library
- Part 41: System Integration Library
C++’s predecessor is C, which debuted in 1972. It is still the most used language with C++ in fourth place and C# in fifth.
C++ got started with the name “C with Classes” in 1979. The name C++ came later in 1982. The original C++ compiler, Cfront, output C source files which were then compiled to machine code. That compiler has long since been replaced and modern compilers all compile C++ directly to machine code.
Major additions to the language were added with “C++ 2.0” in 1989 and the language was then standardized by ISO in 1998. Colloquially, this was called C++98 and began the convention where the year is added to name a version of the language. It also formalized the process of designing and standardizing the language via a committee and various working groups.
Minor changes to the language in 2003 resulted in C++03, but the “Modern C++” era began with huge changes to the language in C++11. This also quickened the standardization process from the previous eight year gap to just three years. This meant that we got minor changes in C++14, relatively big changes in C++17, and huge changes are about to be released in C++20. Game engines such as Unreal, Cryengine, and Lumberyard all support the latest version—C++17—and will likely support C++20 after it is standardized.
At this point the language has little resemblance to C. Much C code will still compile as C++, but idiomatic C++ is only superficially similar to C.
Every release of C++ includes what is called the “Standard Library.” This is often called the “STL,” meaning “Standard Template Library,” for its heavy use of a C++ language feature called templates. This library is also standardized by ISO along with the language itself.
The Standard Library is similar to .NET’s Framework Class Library or CoreFX. The architectural approach is for the C++ language to have powerful, low-level language features so more can be implemented in libraries instead of directly included in the language. For example, the language doesn’t include a string class. Instead, the Standard Library provides a
string class that is efficiently implemented with low-level language features.
The following table shows the major sections of the Standard Library and their loose equivalents in .NET:
|Standard Library Section||C++||C#|
Some game programming environments do not use the Standard Library, or at least minimize its use. EA has implemented their own version called EASTL. Unreal has many built-in similar types (
string) and functions (
make_unique). These libraries benefit from the same low-level language features that the Standard Library is built on, but instead use them to efficiently reimplement what would be language features in many other languages.
The main tool is, of course, the compiler. There are many good options these days, but here are some of the most popular ones:
|Microsoft Visual Studio||Free and Paid||No||Windows|
|GCC (GNU Compiler Collection)||Free||Yes||Windows, macOS, Linux|
|Clang||Free||Yes||Windows, macOS, Linux|
|Intel C++||Free||No||Windows, macOS, Linux|
There are also many IDEs available with the usual combination of features: a text editor, compiler execution, interactive debugger, etc. Here are some popular options:
|Microsoft Visual Studio||Free and Paid||No||Windows|
|JetBrains CLion||Paid||No||Windows, macOS, Linux|
|Microsoft Visual Studio Code||Free||Yes||Windows, macOS, Linux|
Many static analyzers, known as “linters,” and dynamic analyzers are available. The Clang sanitizers suite is free, open source, and has Unreal support. Commercial tools such as Coverity SAST are also available. Clang format and many IDEs can enforce style guides and automatically reformat code.
The C++ standard is available for purchase, but almost no C++ developers actually buy it. A draft version is available for free and will be nearly identical, but it is extremely long and technical so it is also only a reference of last resort. Instead of the standard itself, most developers read reference sites such as cppreference.com just as they would read Microsoft Docs (a.k.a. MSDN) for C# reference.
Many guideline documents exist for C++. The C++ Core Guidelines, Google C++ style guide, and engine-specific standards are all commonly used. The C++ Core Guidelines, in particular, has a companion Guidelines Support Library (GSL) to enforce and facilitate the guidelines.
There are many places where the community of developers congregate. Here are a few:
- The C++ language Slack has 16,000 members
- The /r/cpp subreddit has 136,000 members
- The /r/Cplusplus subreddit has 21,000 members
- CppCon is held annually and posts hundreds of talks
- Engine-specific forums are generally very active
- Many C++ GitHub repositories have 10,000+ stars and active Issues sections
With this introduction out of the way, next week we’ll dive into the language itself and start learning how to program in C++!