public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Defaulted initializer constructor
@ 2012-11-26  9:49 Henrik Mannerström
  2012-11-26 10:31 ` Jonathan Wakely
  0 siblings, 1 reply; 2+ messages in thread
From: Henrik Mannerström @ 2012-11-26  9:49 UTC (permalink / raw)
  To: gcc-help

Hello!

If I write
class MT {
public:
int k[3];
};

then g++(4.6.3) accepts
MT a{{0,1,2}};

but if I add a constructor
MT() {
for (int i = 0;i!=3;i++)
k[i] = 0;
}

then MT a{{0,1,2}}; gives me
error: no matching function for call to ‘MT::MT(<brace-enclosed
initializer list>)Â’

Now, if I add
MT(std::initializer_list<int> list) = default;

I get
error: ‘MT::MT(std::initializer_list<int>)’ cannot be defaulted

What constructor was I (gcc) using earlier and how can I get i back?

BR,
Henrik Mannerstrom

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Defaulted initializer constructor
  2012-11-26  9:49 Defaulted initializer constructor Henrik Mannerström
@ 2012-11-26 10:31 ` Jonathan Wakely
  0 siblings, 0 replies; 2+ messages in thread
From: Jonathan Wakely @ 2012-11-26 10:31 UTC (permalink / raw)
  To: Henrik Mannerström; +Cc: gcc-help

n 26 November 2012 09:48, Henrik Mannerström wrote:
> Hello!
>
> If I write
> class MT {
> public:
> int k[3];
> };
>
> then g++(4.6.3) accepts
> MT a{{0,1,2}};
>
> but if I add a constructor
> MT() {
> for (int i = 0;i!=3;i++)
> k[i] = 0;
> }

This constructor could be written far more easily:

MT() : k() { }

This initializes every element of the array to zero, and stays correct
if you change the array size.

> then MT a{{0,1,2}}; gives me
> error: no matching function for call to ‘MT::MT(<brace-enclosed
> initializer list>)’
>
> Now, if I add
> MT(std::initializer_list<int> list) = default;
>
> I get
> error: ‘MT::MT(std::initializer_list<int>)’ cannot be defaulted
>
> What constructor was I (gcc) using earlier and how can I get i back?

It wasn't using a constructor, you were using aggregate initialization
to provide initial values for each member. A class with a constructor
is not an aggregate, so cannot use aggregate initialization.  Adding a
constructor to your class implies the type is not a simple aggregate,
it needs a manually-written constructor with user-defined effects.

You could define a constructor that accepts arguments:

MT::MT(int i0=0, int i1=0, int i2=0)
: k{i0, i1, i2}
{ }

This constructor works for zero to three arguments, if you don't want
that then have two separate constructors taking zero arguments and
three arguments. Either way, you can do:

MT a{ 0, 1, 2 };
MT b;

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2012-11-26 10:31 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-11-26  9:49 Defaulted initializer constructor Henrik Mannerström
2012-11-26 10:31 ` Jonathan Wakely

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).