C++ 14
C++14 is the informal name given to the iteration of the C++ programming language standard that was released in 2014. It is the fourth revision of the C++ standard after C++98, C++03, and C++11. C++14 brought several new features, enhancements, and improvements over its predecessor C++11, while remaining backward-compatible with existing codebases.
Here are some key features introduced in C++14:
Table of Contents
Binary Literals
C++14 allows you to represent binary values directly using the 0b
or 0B
prefix.
For example,
int binaryValue = 0b101010;.
Generic Lambdas
C++14 extends the capabilities of lambdas by allowing them to have auto parameters, making it easier to write generic code. For instance:
auto add = [](auto a, auto b) { return a + b; };
Return Type Deduction for Functions
In C++14, you can use auto
as the return type of a function to deduce its return type from the return statement. For example:
auto add(int a, int b) { return a + b; } // The return type is deduced as 'int'.
decltype(auto)
This feature allows you to declare a variable with the same type as an expression using the decltype(auto)
syntax. For example:
int x = 42;
decltype(auto) y = x; // y will have the type int, not int&.
Relaxing constexpr
Restrictions
C++14 loosens some restrictions on constexpr
functions, allowing them to have multiple return statements, loops, and local variables.
In C++14, you can use constexpr for functions that call other non-constexpr functions, as long as the non-constexpr function calls occur only under certain conditions.
Variable Templates
C++14 introduces variable templates, which are similar to function templates but define variables instead. For example:
template <typename T>
constexpr T pi = T(3.1415926535897932385);
Digit Separators
C++14 allows the use of single quotes as digit separators in numeric literals, which helps improve readability. For example:
long long bigNumber = 100'000'000;
[[deprecated]] Attribute
C++14 introduces the [[deprecated]]
attribute, which allows you to mark functions, types, or variables as deprecated.
User-Defined Literals for Standard Library Types
C++14 allows user-defined literals for standard library types, enabling you to define custom suffixes for literals of specific types.
std::make_unique
C++14 adds the std::make_unique
function template to create std::unique_ptr
objects with more concise syntax compared to C++11.
These are some of the significant features that C++14 brings to the table. It is essential to note that C++14 builds on top of C++11 and retains its features while introducing new ones. Since C++14 is backward-compatible with C++11, existing codebases written in C++11 should work seamlessly with a C++14 compiler. It is always a good practice to use the latest available standard whenever possible, as it brings improvements, bug fixes, and better language features to help developers write cleaner and more efficient code.