C++11:
When new beginner hears the word C++11
for him it like a technical jargon whom beginner things to learn and know. But you know what C++11
know as C++11
because it was introduce in the year 2011 nothing much scientific about the name .
Understanding C++11
Version of the C++
Programming language which standardized in 2011
is name as "C++11". Without much thinking put one thing in a mind that "C++11" simply refers the year of its introduction without any complex scientific implication
.
What is their in C++11?
C++11 is an updated version of C++
which get standardized in 2011
this standarization has introduced modern features
to make programming easier
, safer
, and faster
. Here are a few key highlights:
- Smart Pointers – Manage memory automatically, so you don’t need to use
new
anddelete
. - Lambdas – Small, inline functions for quick use.
auto
Keyword – Lets the compiler figure out variable types.- Range-based
for
Loops – A simpler way to loop through collections. nullptr
– A better, safer replacement forNULL
.- Concurrency – Basic support for multi-threading with tools like
std::thread
. - Move Semantics – Makes copying big objects faster.
Overview:
As I am just learning and sharing my knowledge and everyone do the same. All the content on this blog has been derived from the various sources (see the [Reference] section) for more details about the reference taken to write this article. A salute to all of the open source enthusiast and the knowledge sharing people who belive in the concept of knowledge should be free
.
Features:
C++11 includes the following new language features:-
- attributes
- auto
- char32_t and char16_t
- converting constructors
- constexpr
- decltype
- default functions
- deleted functions
- delegating constructors
- explicit conversion functions
- explicit virtual overrides
- final specifier
- forwarding references
- initializer lists
- inline-namespaces
- lambda expressions
- move semantics
- noexcept specifier
- non-static data member initializers
- nullptr
- range-based for loops
- raw string literals
- ref-qualified member functions
- right angle brackets
- rvalue references
- special member functions for move semantics
- static assertions
- strongly-typed enums
- trailing return types
- type aliases
- user-defined literals
- variadic templates
C++11 includes the following new library features:
- memory model
- smart pointers
- std::array
- std::async
- std::begin/end
- std::chrono
- std::forward
- std::make_shared
- std::move
- std::ref
- std::thread
- std::tie
- std::to_string
- tuples
- type traits
- unordered containers
C++11 Language Features:
Attributes:
In C++11, attributes provide metadata to the compiler for optimizations and behavior control. They are written in [[ ]]
and can be applied to functions, variables, etc.
Common Attributes:
[[noreturn]]
: Marks functions that don’t return.[[deprecated]]
: Marks functions or types as deprecated.[[nodiscard]]
: Warns if the return value is ignored.[[maybe_unused]]
: Suppresses warnings for unused variables.
Example:
[[deprecated]] void old_function() { }
[[nodiscard]] int compute() { return 42; }
Attributes help with optimizations, error checking, and warnings.
auto:
Variables declared with the auto
keyword enable the compiler to automatically determine their data type based on the expression assigned to them during initialization. This feature simplifies code by reducing the need for explicit type declarations, allowing for cleaner and more concise syntax.
For instance, if you initialize a variable with an integer value, the compiler will deduce its type as int
. Similarly, if you assign a floating-point number, it will infer the type as float
or double
accordingly. This not only saves developers time but also helps prevent type-related errors by ensuring that the variable’s type is consistently derived from its initial value.
Moreover, auto
enhances code maintainability, especially in cases involving complex types, such as iterators or lambda expressions, where the type might be cumbersome to write out explicitly.
Example:
auto count = 10; // count is deduced as int
auto price = 19.99; // price is deduced as double
auto name = "CompilerSutra"; // name is deduced as const char*
auto variable = { 0 }; // std::initializer_list<int>
auto required_initializer; // as here no data type is initialized so compile will throw error as{required_initiailizer}
In each case, the compiler takes the initializer's type and assigns it to the variable, allowing developers to focus more on the logic rather than type management.
char32_t and char16_t:
char16_t
and char32_t
are types introduced in C++11 to represent UTF-16 and UTF-32 encoded characters, respectively. They enable the representation of a wide range of Unicode characters, making it easier to handle international text.
Example:
char16_t utf16_char = u'你'; // A Unicode character in UTF-16
char32_t utf32_char = U'😊'; // A Unicode character in UTF-32
converting constructors:
Converting constructors are special constructors in C++ that allow for implicit conversion from one type to another when initializing an object. They provide a way to create an object of a class from an object of a different type, enabling flexible type conversions without requiring an explicit cast.
For example, if you have a class Point
that represents a 2D point using integer coordinates, you could define a converting constructor that accepts a double
to facilitate the creation of a Point
object from a double
representing a point's coordinate.
Example:
class Point {
public:
int x, y;
// Converting constructor
Point(double coord) {
x = static_cast<int>(coord);
y = static_cast<int>(coord);
}
};
Point p = 5.7; // Implicit conversion from double to Point
constexpr:
constexpr
is a keyword introduced in C++11 that indicates that a function or variable can be evaluated at compile time. This allows for optimizations and can lead to more efficient code by enabling the compiler to perform computations during the compilation process instead of at runtime.
Using constexpr
can significantly improve performance, especially in contexts where the result of a computation is needed repeatedly and can be determined beforehand.
Example:
constexpr int square(int x) {
return x * x; // This function can be evaluated at compile time
}
constexpr int result = square(5); // result is evaluated at compile time
decltype:
decltype
is a keyword introduced in C++11 that allows you to query the type of an expression at compile time. This feature is particularly useful in template programming and when working with complex types, as it helps to avoid manual type declarations and enhances type safety.
Key Features:
- Type Deduction:
decltype
deduces the type of an expression without evaluating it. This is useful for ensuring that variables and return types are correctly typed based on existing expressions. - constexpr and Reference::
decltype
preserves the type properties of the expression, including constness and reference qualifiers. For example, if you usedecltype
on an lvalue, it yields a reference type; if you use it on an rvalue, it yields a non-reference type.
Example:
int x = 42;
decltype(x) y = 10; // y is deduced to be of type int
const int& z = x;
decltype(z) w = z; // w is deduced to be const int& (a reference type)
Default functions:
In C++11, a default function is a member function that the compiler automatically provides if no user-defined version is declared, including constructors, destructors, and copy/move assignment operators. Developers can explicitly request a default implementation by using the = default;
syntax, which allows for cleaner code while ensuring correct behavior in resource management and object copying.
Example:
class MyClass {
public:
// Default constructor
MyClass() = default;
// Default destructor
~MyClass() = default;
// Defaulted copy constructor
MyClass(const MyClass&) = default;
// Defaulted copy assignment operator
MyClass& operator=(const MyClass&) = default;
};