Key New Features in C++11

1. long long Integer Type

The long long type is an extended 64-bit integer type (8 bytes) with a signed range of -2^63 to 2^63 - 1.

To explicitly denote a value as a signed long long, append the suffix LL or II (e.g., 10LL represents the signed 64-bit integer 10). For unsigned long long values, use the suffix ULL or UII (e.g., 10ULL for the unsigned 64-bit integer 10).

To check platform-specific range limits for long long types, use these standard macros:

  • LLONG_MIN: Minimum value for signed long long
  • LLONG_MAX: Maximum value for signed long long
  • ULLONG_MAX: Maximum value for unsigned long long (minimum is always 0)

2. List Initialization

C++11 universalizes {} for initialization, called list initialization, which works across all data types.

#include <iostream>
#include <vector>
#include <map>
#include <string>

using namespace std;

class MyClass {
public:
    MyClass(int) {}
    ~MyClass() {}
};

int main() {
    int zeroInit{};
    int oneInit{1};
    int altOne = {1};
    double preciseVal = double{3.14159};

    string charSeq{'h','e','l','l','o'};
    string subSeq{charSeq, 1, 3};
    string mixedInit{0x62, 'a'};
    string strLiteral{"C++11 List Initialization"};
    
    // Dynamic memory allocation with list initialization
    int* dynInt = new int{42};
    int* dynArr = new int[5]{10, 20};
    
    // Class object initialization
    MyClass obj1 = {7};
    MyClass obj2{7};
    
    cout << "List initialization demo complete\n";
    return 0;
}

List initialization is uniform for primitive types, arrays, STL containers, and class objects.

3. nullptr Constant

nullptr is an rvalue constant of type nullptr_t, designed to initialize null pointers and avoid ambiguity with NULL (which resolves to 0):

#include <iostream>
#include <cstddef>

using namespace std;

void processValue(int num) {
    cout << "Processing integer value: " << num << endl;
}

void processValue(void* ptr) {
    cout << "Processing pointer value" << endl;
}

int main() {
    int* intPtr = nullptr;
    char* charPtr = nullptr;
    double* doublePtr = nullptr;

    processValue(0);       // Calls int overload
    processValue(NULL);    // Also calls int overload (unintended)
    processValue(nullptr); // Correctly calls void* overload
    
    return 0;
}

nullptr implicitly converts to any pointer or member pointer type and supports equality/inequality comparisons with other pointers.

4. constexpr (Constant Expressions)

The constexpr keyword evaluates expressions at compile time, reducing runtime overhead by eliminating repeated calculations. It modifies variables, functions, constructors, and templates.

(1) constexpr Variables

constexpr int Total = 5 + 7 + 9;
int numbers[Total] = {0};

(2) constexpr Functions

A valid constexpr functon must:

  • Return a non-void type
  • Contain only using, typedef, static_assert, and one return statement
  • Have its definition visible before use
  • Return a constant expression

Example:

#include <iostream>
using namespace std;

constexpr int calculateSum(int x) {
    return 1 + 2 + x;
}

int main() {
    constexpr int FixedSize = 3 + 4;
    int arr[FixedSize] = {0};
    int resultArr[calculateSum(2)] = {1,2,3,4,5};
    cout << resultArr[3] << endl;
    
    return 0;
}

(3) constexpr Constructors

Define a constexpr constructor with an empty body and initialize members via an initializer list:

struct Person {
    constexpr Person(const char* name, int age) : name(name), age(age) {}
    const char* name;
    int age;
};

(4) constexpr Template Functions

template <typename T>
constexpr T getValue(T input) {
    return input;
}

(5) constexpr Pointers

Note the difference from const:

const int* constPtr = nullptr; // Pointer to constant integer
constexpr int* ptrConst = nullptr; // Constant pointer to integer (equivalent to int* const ptrConst = nullptr)

5. Type Alias Declaration with using

C++11 simplifies type aliases with using, more intuitive than typedef for templates:

template<typename T>
using StringMap = std::map<std::string, T>;

// Usage:
StringMap<int> ageMap;

6. auto Automatic Type Deduction

The auto keyword deduces a variable's type at compile time, requiring immediate initialization:

auto num = 42;
auto pi = 3.14159;
auto ptr = &num;
auto siteUrl = "https://example.com/";
  • num is deduced as int
  • pi as double
  • ptr as int*
  • siteUrl as const char*

7. decltype Type Deduction

decltype deduces a type from an expression (not the initial value), unlike auto, and does not require immediate initialization:

// Syntax: decltype(expression) variableName = value;
int x = 10;
decltype(x) y = 20; // y is int
decltype(x + y) z = 30; // z is int (since x+y is int)

decltype is useful when the desired type depends on an expression's result rather than a direct initializer.

Tags: C++11 C++ Programming software development Programming Languages C++ Features

Posted on Thu, 14 May 2026 21:42:19 +0000 by VirtualOdin