Say I have this structure:
struct F
{
int& ref; // reference member
const int c; // const member
// F::F() is implicitly defined as deleted
};
That is from cppreference. As I understand from the documentation the constructor of F
is considered deleted because it has a reference variable which refers to nothing. So one cannot declare a variable of type F
like so: F variableName;
as there will be errors such as: uninitialized reference member in struct F
.
I understand this however I do not understand what such a structure would be good for if you cannot even declare a variable of its type. Could such a data type be useful in some specific case?
Since F
is an aggregate you can use aggregate initialization:
int a = 42;
F f1 = {a, 13};
// or
F f2{a, 9};
A class type (typically, struct or union) is an aggregate if it has:
- no private or protected non-static data members
- no user-provided, inherited, or explicit (since C++17) constructors (explicitly defaulted or deleted constructors are allowed) (since C++11)
- no virtual, private, or protected (since C++17) base classes
- no virtual member functions