C++11 Defaulted and Deleted Functions

beginner
21 min

C++11 Defaulted and Deleted Functions

Welcome to this comprehensive lesson on C++11 Defaulted and Deleted Functions! In this tutorial, we'll be diving deep into understanding these powerful features, and learn how to leverage them in your own projects.

By the end of this lesson, you'll be able to:

  • Understand the need for defaulted and deleted functions
  • Learn how to declare and implement defaulted functions
  • Understand the concept of deleted functions
  • Discover real-world use cases for defaulted and deleted functions

Why Defaulted and Deleted Functions? šŸ’”

Before C++11, when writing a class, you had to explicitly write all the special member functions, such as constructors, copy constructors, destructors, assignment operators, etc. This was not only tedious but also prone to errors.

Defaulted functions (= default) and deleted functions (= delete) were introduced in C++11 to simplify this process. They allow you to:

  • Provide default implementations for special member functions
  • Indicate that a special member function should not be generated

Defaulted Functions šŸŽÆ

Defaulted functions are member functions that have their default implementations provided by the compiler. This means you can write a function header only, and the compiler will generate the default implementation for you.

How to Declare a Defaulted Function

To declare a defaulted function, use the = default keyword after the function declaration.

cpp
struct MyStruct { MyStruct() = default; // Default constructor MyStruct(const MyStruct&) = default; // Copy constructor MyStruct& operator=(const MyStruct&) = default; // Copy assignment operator };

When to Use Defaulted Functions

  • When you want to use the compiler-generated implementation
  • To avoid writing unnecessary code and reduce the chance of errors

šŸ“ Note: Defaulted functions are generated even if you provide a user-defined implementation. This can lead to confusion, so it's essential to understand when a defaulted function is being used.

Deleted Functions šŸŽÆ

Deleted functions are member functions that are explicitly marked as not being generated by the compiler. This is useful when you want to prohibit the use of a particular member function.

How to Declare a Deleted Function

To declare a deleted function, use the = delete keyword after the function declaration.

cpp
struct MyStruct { MyStruct(const MyStruct&) = delete; // Copy constructor is deleted MyStruct& operator=(const MyStruct&) = delete; // Copy assignment operator is deleted };

When to Use Deleted Functions

  • To prevent the generation of a special member function
  • To enforce a specific behavior, such as using move constructors and assignment operators instead of copy ones

šŸ“ Note: Deleted functions cannot be called, and attempting to do so will result in a compile-time error.

Real-world Use Cases šŸŽÆ

Defaulted and deleted functions are powerful tools for writing cleaner, more efficient code. Here are some real-world use cases:

  1. Copy and Move Operations

    • Copy constructors and assignment operators are expensive in terms of performance, especially for large objects.
    • By default constructing and assigning from references, copy operations can be costly.
    • Use move constructors and assignment operators instead, and mark copy operations as deleted to enforce this behavior.
  2. Constructors and Assignment Operators for Standard Library Types

    • Standard library types such as std::vector have default constructors, copy constructors, and assignment operators.
    • These functions are often optimized for performance, and you can leverage them in your own classes.
    • Use defaulted constructors to inherit the behavior from standard library types.
  3. Initialization and Assignment of Base Classes

    • When initializing a derived class with a base class, the derived class's constructor is called before the base class's constructor.
    • This can lead to issues when the base class needs to be initialized with a user-defined object.
    • Use defaulted constructors and assignment operators to simplify this process.

Quiz šŸŽÆ

Quick Quiz
Question 1 of 1

What is the purpose of a defaulted function in C++11?

Quick Quiz
Question 1 of 1

How can you indicate that a special member function should not be generated in C++11?