Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8922283
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:47:38+00:00 2026-06-15T06:47:38+00:00

Possible Duplicate: What’s the point in defaulting functions in C++11? C++11 introduced defaulted methods

  • 0

Possible Duplicate:
What’s the point in defaulting functions in C++11?

C++11 introduced defaulted methods (e.g. void myMethod() = default;).

What does it do to methods (how do methods behave after being defaulted). How do I use them properly (what are its uses)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T06:47:39+00:00Added an answer on June 15, 2026 at 6:47 am

    There are a number of class members that are considered “special member functions” by the C++ standard. These are:

    • The default constructor (a constructor that can be called with no parameters).
    • The copy constructor (a constructor that can be called with one parameter that is the object type as an lvalue reference).
    • The copy assignment operator (an operator= overload that can be called with one parameter that is the object type as either an lvalue reference or a value).
    • The move constructor (a constructor that can be called with one parameter that is the object type as an rvalue reference).
    • The move assignment operator (an operator= overload that can be called with one parameter that is the object type as either an rvalue reference or a value).
    • The destructor.

    These member functions are special in that the language does special things with them on types. Another thing that makes them special is that the compiler can provide definitions for them if you do not. These are the only functions that you can use the = default; syntax on.

    The issue is that the compiler will only provide a definition under certain conditions. That is, there are conditions under which a definition will not be provided.

    I won’t go over the entire list, but one example is what others have mentioned. If you provide a constructor for a type that is not a special constructor (ie: not one of the constructors mentioned above), a default constructor will not be automatically generated. Therefore, this type:

    struct NoDefault
    {
      NoDefault(float f);
    };
    

    NoDefault cannot be default constructed. Therefore, it cannot be used in any context where default construction is needed. You can’t do NoDefault() to create a temporary. You can’t create arrays of NoDefault (since those are default constructed). You cannot create a std::vector<NoDefault> and call the sizing constructor without providing a value to copy from, or any other operation that requires that the type be DefaultConstructible.

    However, you could do this:

    struct UserDefault
    {
      UserDefault() {}
      UserDefault(float f);
    };
    

    That would fix everything, right?

    WRONG!

    That is not the same thing as this:

    struct StdDefault
    {
      StdDefault() = default;
      StdDefault(float f);
    };
    

    Why? Because StdDefault is a trivial type. What does that mean? I won’t explain the whole thing, but go here for the details. Making types trivial is often a useful feature to have, when you can do it.

    One of the requirements of a trivial type is that it does not have a user-provided default constructor. UserDefault has a provided one, even though it does the exact same thing as the compiler-generated one would. Therefore, UserDefault is not trivial. StdDefault is a trivial type because = default syntax means that the compiler will generate it. So it’s not user-provided.

    The = default syntax tells the compiler, “generate this function anyway, even if you normally wouldn’t.” This is important to ensure that a class is a trivial type, since you cannot actually implement special members the way the compiler can. It allows you to force the compiler to generate the function even when it wouldn’t.

    This is very useful in many circumstances. For example:

    class UniqueThing
    {
    public:
      UniqueThing() : m_ptr(new SomeType()) {}
      UniqueThing(const UniqueThing &ptr) : m_ptr(new SomeType(*ptr)) {}
      UniqueThing &operator =(const UniqueThing &ptr)
      {
        m_ptr.reset(new SomeType(*ptr)); return *this;
      }
    
    private:
      std::unique_ptr<SomeType> m_ptr;
    };
    

    We must write every one of these functions to make the class copy the contents of the unique_ptr; there’s no way to avoid that. But we also want this to be moveable, and the move constructor/assignment operators won’t automatically be generated for us. It’d silly to re-implement them (and error-prone if we add more members), so we can use the = default syntax:

    class UniqueThing
    {
    public:
      UniqueThing() : m_ptr(new SomeType()) {}
      UniqueThing(const UniqueThing &ptr) : m_ptr(new SomeType(*ptr)) {}
      UniqueThing(UniqueThing &&ptr) = default;
      UniqueThing &operator =(const UniqueThing &ptr)
      {
        m_ptr.reset(new SomeType(*ptr)); return *this;
      }
    
      UniqueThing &operator =(UniqueThing &&ptr) = default;
    
    private:
      std::unique_ptr<SomeType> m_ptr;
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: How is floating point stored? When does it matter? Using the built-in
Possible Duplicate: What does the exclamation mark do before the function? What's the point
Possible Duplicate: ant error JAVA_HOME does not point to SDK I'm getting the following
Possible Duplicate: Round a double to 2 significant figures after decimal point I know
Possible Duplicate: What is the point of function pointers? I saw this code void
Possible Duplicate: Strange floating-point behaviour in a Java program Why does JSP/JSTL division by
Possible Duplicate: What's the point of the var keyword? What advantages does using var
Possible Duplicate: Round a double to 2 significant figures after decimal point The code
Possible Duplicate: what's the point in malloc(0)? what does malloc(0) return? this code displays
Possible Duplicate: Round a double to 2 significant figures after decimal point i've got

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.