Initializing a struct with aggregate initialization and member initializers [duplicate]
Asked Answered
A

2

18

Consider the following example:

#include <iostream>
#include <string>
struct ABC
{
    std::string str;
    unsigned int id ;/* = 0 : error: no matching constructor for initialization of 'ABC'*/
};

int main()
{
    ABC abc{"hi", 0};
    std::cout << abc.str << " " << abc.id <<   std::endl;
    return 0;
}

When defining the structure ABC without default value for id clang 3.x and gcc 4.8.x compile the code without problems. However, after adding a default argument for "id" I get the flowing error message:

13 : error: no matching constructor for initialization of 'ABC'
ABC abc{"hi", 0};
^ ~~~~~~~~~
4 : note: candidate constructor (the implicit copy constructor) not viable: requires 1 argument, but 2 were provided
struct ABC
^
4 : note: candidate constructor (the implicit move constructor) not viable: requires 1 argument, but 2 were provided
4 : note: candidate constructor (the implicit default constructor) not viable: requires 0 arguments, but 2 were provided
1 error generated.
Compilation failed

From a technical point of view, what is going on when I define id with a default argument and why is aggregate initialization not possible in that case? Do I implicitly define some sort of constructor?

Aq answered 9/3, 2016 at 12:10 Comment(7)
Maybe I'm misunderstanding your question. Is this what you wanted?Saxena
@Saxena That's interesting. It fails to compile in VS 2015.Loewi
Just found this thread in regards to VS 2015. Looks like it might be the same issue for your compiler.Loewi
@Saxena This works fine with C++14 and seems not to work for C++11.Coated
@user3472628: See my answer for the reason.Tangram
@Tangram Chapeau bas my friend.Coated
uuplicate, #27119035Kevyn
T
33

Bjarne Stroustrup and Richard Smith raised an issue about aggregate initialization and member-initializers not working together.

The definition of aggregate is slightly changed in C++11 & C++14 standard.

From the C++11 standard draft n3337 section 8.5.1 says that:

An aggregate is an array or a class (Clause 9) with no user-provided constructors (12.1), no brace-or-equal- initializers for non-static data members (9.2), no private or protected non-static data members (Clause 11), no base classes (Clause 10), and no virtual functions (10.3).

But C++14 standard draft n3797 section 8.5.1 says that:

An aggregate is an array or a class (Clause 9) with no user-provided constructors (12.1), no private or protected non-static data members (Clause 11), no base classes (Clause 10), and no virtual functions (10.3).

So, when you use in class member initializer (i.e. equal initializer) for the data member id in C++11 it no longer remains aggregate & you can't write ABC abc{"hi", 0}; to initialize a struct ABC. Because it no longer remains aggregate type after that. But your code is valid in C++14. (See live demo here).

Tangram answered 9/3, 2016 at 12:44 Comment(3)
Excellent comparison between standards. +1Saxena
For anyone who is interested in seeing a list of C++11/14/17 features that Visual Studio 2015 supports. Note that it does NOT support NSDMIs for aggregates.Loewi
Thanks for pointing that out. That really helped! +1Aq
D
-1

In c++ struct and classes are the same, except that structs have default public members and classes have private. If you want to use initial values I think you have to write a constructor or use something like this:

struct ABC
{
    std::string str;
    unsigned int id;
} ABC_default = {"init", 0 }; //initial values

int main()
{
    ABC abc = ABC_default;
    std::cout << abc.str << " " << abc.id << std::endl;
    return 0;
}
Dieldrin answered 9/3, 2016 at 12:34 Comment(4)
I don't really think this answers OP's question.Interbreed
It's also wrong, since NSDMI were added to the language five years ago. The C-like syntax doesn't help the answer either. And not to sound like a broken record or anything but "struct and classes" aren't "the same"; struct introduces a class and structs don't exist!Kazmirci
By saying that "struct and classes are the same" I mean that they are the same kind, only the keyword are different. Thanks for the replyKetone
This is really inflexible. What is someone wants to initialize ABC multiple times? Will he have to constantly keep specifying a new type of ABC in the definition of ABC?Down

© 2022 - 2024 — McMap. All rights reserved.