If I have
class Something
{
public:
Something(int whatever) : whatever_(whatever) {}
private:
int whatever_;
}
Then what happens when I create an object on the stack
Something something;
since there is no default constructor?
On a conforming compiler you get a compilation error.
The following code:
class Something
{
public:
Something(int whatever) : whatever_(whatever) {}
private:
int whatever_;
};
Something something;
When compiled with gcc8.2 results in the following compilation error:
<source>:9:11: error: no matching function for call to 'Something::Something()'
Something something;
^~~~~~~~~
<source>:4:5: note: candidate: 'Something::Something(int)'
Something(int whatever) : whatever_(whatever) {}
^~~~~~~~~
<source>:4:5: note: candidate expects 1 argument, 0 provided
<source>:1:7: note: candidate: 'constexpr Something::Something(const Something&)'
class Something
^~~~~~~~~
<source>:1:7: note: candidate expects 1 argument, 0 provided
<source>:1:7: note: candidate: 'constexpr Something::Something(Something&&)'
<source>:1:7: note: candidate expects 1 argument, 0 provided
Compiler returned: 1
Live example available at godbolt.