Questions tagged [defaulted-functions]

Questions related to use of C++ compiler-generated functions, generated implicitly, or explicitly by use of = default.

19 questions
22
votes
8 answers

What's the point in defaulting functions in C++11?

C++11 adds the ability for telling the compiler to create a default implementation of any of the special member functions. While I can see the value of deleting a function, where's the value of explicitly defaulting a function? Just leave it blank…
Motti
  • 110,860
  • 49
  • 189
  • 262
16
votes
1 answer

=default ignores access specifier?

I found it quite odd that the following program still compiled fine despite the default constructor being private (4.8.1 g++): class A{ private: A() = default; A(const A&) = default; }; int main(){ A a; } Actually from…
Silversonic
  • 1,289
  • 2
  • 11
  • 26
11
votes
1 answer

Can I default a private constructor in the class body or not?

GCC 4.5 doesn't let me do this: class foo { public: foo() = default; private: foo(foo const&) = default; foo& operator=(foo const&) = default; }; It complains that: error: 'foo::foo(const foo&)' declared with non-public access cannot…
R. Martinho Fernandes
  • 228,013
  • 71
  • 433
  • 510
8
votes
1 answer

Can special member functions be defaulted if they use typedefs?

Clang compiles this fine, but GCC and MSVC complain that operator= cannot be defaulted: #include template struct S { typedef typename std::enable_if::value, S>::type Me; S &operator=(Me const &) =…
user541686
  • 205,094
  • 128
  • 528
  • 886
8
votes
2 answers

Are user-defined default constructors less efficient?

Some days ago, while reading Standard C++ news I've read the post about Defaulted functions in C++11, in that article is mentioned that the user-defined constructor is less efficient than the one generated by the compiler: The user-defined default…
PaperBirdMaster
  • 12,806
  • 9
  • 48
  • 94
7
votes
2 answers

Why is the constructor of std::in_place_t defaulted and explicit?

cppreference shows the following definition of std::in_place_t: struct in_place_t { explicit in_place_t() = default; }; inline constexpr std::in_place_t in_place{}; Why have they added an explicit and defaulted constructor? Why it isn't left…
7
votes
2 answers

Why would you "default" a copy/move constructor or a destructor?

C++0x lets you specify certain functions as defaulted: struct A { A() = default; // default ctor A(A const&) = default; // copy ctor A(A&&) = default; // move ctor A(Other); // other ctor ~A() = default; …
Roger Pate
4
votes
3 answers

How can one default a special member function if one doesn't know its parameter types?

Consider this case: template struct A { A(A ???&) = default; A(A&&) { /* ... */ } T t; }; I explicitly declared a move constructor, so I need to explicitly declare a copy constructor if I want to have a non-deleted copy…
Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
4
votes
0 answers

using std::unique_ptr pimpl with explicit default destructor

When defining the following class class Foo { public: Foo (void); ~Foo (void) = default; protected: class FooImpl; std::unique_ptr _impl; //... }; Foo::Foo (void) : _impl (std::make_unique ()) { } I get the…
Daniel Heilper
  • 1,182
  • 2
  • 17
  • 34
3
votes
1 answer

Why C++20 allows defaulted comparison to compile even when it is deleted?

Consider the following code: struct A { }; struct B { A a; bool operator == (const B& other) const = default; }; clang gives a nice warning : warning: explicitly defaulted equality comparison operator is implicitly deleted…
NoSenseEtAl
  • 28,205
  • 28
  • 128
  • 277
3
votes
3 answers

C++11 deleted/defaulted constructors

I'm a bit confused about how/why the constructors are called in C++11 and C++17. #include using namespace std; //--- template struct StructTest { public: const T Var = -1; //--- // constexpr StructTest() = delete; …
2
votes
1 answer

Why does a defaulted default constructor depend on whether it is declared inside or outside of class?

In the following example, struct A does not have default constructor. So both struct B and struct C inherited from it cannot get compiler-generated default constructor: struct A { A(int) {} }; struct B : A { B() = default; //#1 }; struct C…
Fedor
  • 17,146
  • 13
  • 40
  • 131
2
votes
1 answer

Visual Studio C2580 when using default keyword

For the following code: struct S { S() = default; S(S const&) = default; S(S&&) = default; S& operator=(S const& other) = default; S& operator=(S&&) = default; template S(T&&... params) { …
Peter Lenkefi
  • 1,306
  • 11
  • 29
1
vote
2 answers

Why does copy-and-swap in a base class cause the copy-assignment operator to be implicitly deleted in the derived class?

Tested only in GCC and Clang, the presence of a pass-by-value copy assignment operator in the base class (useful when implementing the copy-and-swap (or copy-and-move) idiom) causes the copy assignment operator in the derived class to be implicitly…
1
vote
1 answer

C++ implicit definition of special functions

In the current version of the C++ draft (september 2019), paragraph [class.default.ctor]/4 states: A default constructor that is defaulted and not defined as deleted is implicitly defined when it is odr-used ([basic.def.odr]) to create an object of…
1
2