Default constructor vs implicit constructor

Someone may have already asked about this, but the default search is for "default", "defaulted", "explicit", etc. does not give good results. But anyway.

I already know that there are some differences between an explicitly set default constructor (i.e. without arguments) and an explicitly defined default constructor (i.e. with a keyword default), from here: New keyword = default in C ++ 11

But what are the differences between an explicitly defined constructor with default and implicit definition (i.e. when the user does not write it at all)?

class A
{
public:
    A() = default;
    // other stuff
};

against

class A
{
    // other stuff
};

, , , , , . ?

: , A() = default; , ( , ).

+4
2

= default - . , - .

  • / public, .

  • . :

    $ cat a.cpp 
    class A
    {
    public:
        [[deprecated]] A() = default;
    };
    
    int main()
    {
        A a;
    }
    
    $ g++ -std=c++14 a.cpp
    a.cpp: In function ‘int main()’:
    a.cpp:9:7: warning: ‘constexpr A::A()’ is deprecated [-Wdeprecated-declarations]
         A a;
           ^
    a.cpp:4:20: note: declared here
         [[deprecated]] A() = default;
                        ^
    
+2

C++ 4- , .


, . , , . , :

class gslice {
    valarray<size_t> size;
    valarray<size_t> stride;
    valarray<size_t> d1;
public:
    gslice() = default;    //<< Explicit default constructor
    ~gslice() = default;
    gslice(const gslice&) = default;
    gslice(gslice&&) = default;
    gslice& operator=(const gslice&) = default;
    gslice& operator=(gslice&&) = default;
    // ...
};

std :: gslice (§40.5.6) :

class gslice {
    valarray<size_t> siz e;
    valarray<size_t> stride;
    valarray<size_t> d1;
public:
    // ...
};
0

Source: https://habr.com/ru/post/1664141/


All Articles