How to Install C++ 23 on macOS
C++23 is the latest update to the C++ programming language standard, following C++20. As with each update, C++23 aims to introduce new features and improvements over its predecessor, focusing on enhancing the language’s usability, performance, and safety.
The following are some of the updates and features in C++ 23.
Library Improvements
std::expected<T, E>
- A template class for representing values that may either be a valid result of typeT
or an error of typeE
.std::mdspan
- A multidimensional view for arrays, allowing for more flexible handling of multi-dimensional data without copying it.
Language Features
- Deduction Guides for Aggregate Initialization - This feature will allow class template argument deduction (CTAD) to be used with aggregates, making it easier to use templates with aggregate types.
if consteval
- A new conditional statement that checks if the context is a compile-time evaluation, improving the ability to write code that is conditionally compiled based on whether it’s being evaluated at compile-time or run-time.[[nodiscard("reason")]]
attribute - Enhancements to the[[nodiscard]]
attribute, allowing developers to specify a reason why the return value of a function should not be ignored.
Syntax Improvements
- Uniform initialization syntax for
new
expressions - This aims to allow uniform initialization syntax (using braces) innew
expressions, making initialization syntax more consistent across the language. - Pattern Matching - Inspired by pattern matching in other languages, this would introduce a way to perform pattern-based matching and binding against values, although its inclusion in C++23 is uncertain.
Concurrency and Parallelism Enhancements
- Executors - A framework for managing execution contexts and resources, crucial for writing portable and efficient concurrent and parallel code. The inclusion of executors in C++23 has been a topic of discussion and may be postponed to future revisions.
You can learn more about the features in the link below.
https://en.wikipedia.org/wiki/C%2B%2B23
MacOS Enable C++ 23
It is good to keep in mind that C++ is very new and hence not all compilers support it. When it comes to GNU compilers, C++ is only supported by GCC 11.
Install GCC 11
Therefore, you will need to ensure that you have GCC version 11 installed on your system. You can install it by running the command:
brew install gcc@11
This command tells Homebrew to install the GCC version 11 package.
After the installation is complete, you might need to link GCC 11 if it’s not the default compiler. You can do this by running:
brew link gcc@11 --force
However, linking with --force
might not be a good idea if you want to keep the system’s default compiler settings.
Instead, we can add GCC 11 to your PATH
manually by adding the following line to your .bash_profile
, .zshrc
, or equivalent shell configuration file:
export PATH="/opt/homebrew/Cellar/gcc@11/11.4.0/bin:$PATH"
You can confirm the installation of GCC 11 using the command:
g++-11 --version
Output:
g++-11 (Homebrew GCC 11.4.0) 11.4.0
Copyright (C) 2021 Free Software Foundation, Inc.
This is free software; see the source for copying conditions...
Compile with C++ 23
To compile a CPP program using version 23, you can run the command:
g++-11 --std=c++23 -o a.out hello.cpp
This should compile the C++ program with version 23.
Conclusion
In this tutorial, we learned how to install and enable support for C++ 23 on macOS using the GCC compiler.
:) Later Gator!