Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > table initialization

Reply
Thread Tools

table initialization

 
 
AdlerSam
Guest
Posts: n/a
 
      02-16-2011
Hi,

please consider the following example:

struct A {
A(int) {}
A() {}
};
struct B {
B(const A &a): _a(a) {}
B(int x, const A &a): _a(a) {}
void f() {}
A _a;
};
int main() {
B v[] = {
B(A(3)),
B(27, A()),
B(A())
};
v[0].f();
v[1].f();
v[2].f();

B x(A(3));
B y(27, A());
B z(A());
x.f();
y.f();
z.f();
}

The last line doesn't compile:

tst.cpp:26: error: request for member f in z, which is of non-
class type B ()(A (*)())

Obviously, because the compiler thinks that z is a function.

While it is easy to work arround the problem with an intermediate
variable for class B, it annoys me because I need to instantiate a
rather big amount of objects, an I'd like to do it in a table-like
manner.

Thus, my question: Is there a way to tell the compiler that z is an
object while keeping the table-based initialization sceme?

Thanks,

Sam



 
Reply With Quote
 
 
 
 
Saeed Amrollahi
Guest
Posts: n/a
 
      02-16-2011
On Feb 16, 6:11*pm, AdlerSam <(E-Mail Removed)> wrote:
> Hi,
>
> please consider the following example:
>
> struct A {
> * * A(int) {}
> * * A() {}};
>
> struct B {
> * * B(const A &a): _a(a) {}
> * * B(int x, const A &a): _a(a) {}
> * * void f() {}
> * * A _a;};
>
> int main() {
> * * B v[] = {
> * * * * B(A(3)),
> * * * * B(27, A()),
> * * * * B(A())
> * * };
> * * v[0].f();
> * * v[1].f();
> * * v[2].f();
>
> * * B x(A(3));
> * * B y(27, A());
> * * B z(A());
> * * x.f();
> * * y.f();
> * * z.f();
>
> }
>
> The last line doesn't compile:
>
> tst.cpp:26: error: request for member f in z, which is of non-
> class type B ()(A (*)())
>
> Obviously, because the compiler thinks that z is a function.
>
> While it is easy to work arround the problem with an intermediate
> variable for class B, it annoys me because I need to instantiate a
> rather big amount of objects, an I'd like to do it in a table-like
> manner.
>
> Thus, my question: Is there a way to tell the compiler that z is an
> object while keeping the table-based initialization sceme?
>
> Thanks,
>
> Sam


Hi

It's about "The Most Vexing Parse" problem.
C++ has parsing ambiguity between a function declaration and a
constructor invocation when the constructor arguments are temporaries.
This is known as the "most vexing parse" problem.
Put the temporary object inside (). I mean
B z((A()));
For further information, please see
http://www.informit.com/guides/conte...lus&seqNum=439
and
http://en.wikipedia.org/wiki/Most_vexing_parse

HTH,
-- Saeed Amrollahi
 
Reply With Quote
 
 
 
 
Saeed Amrollahi
Guest
Posts: n/a
 
      02-16-2011
On Feb 16, 6:11*pm, AdlerSam <(E-Mail Removed)> wrote:
> Hi,
>
> please consider the following example:
>
> struct A {
> * * A(int) {}
> * * A() {}};
>
> struct B {
> * * B(const A &a): _a(a) {}
> * * B(int x, const A &a): _a(a) {}
> * * void f() {}
> * * A _a;};
>
> int main() {
> * * B v[] = {
> * * * * B(A(3)),
> * * * * B(27, A()),
> * * * * B(A())
> * * };
> * * v[0].f();
> * * v[1].f();
> * * v[2].f();
>
> * * B x(A(3));
> * * B y(27, A());
> * * B z(A());
> * * x.f();
> * * y.f();
> * * z.f();
>
> }
>
> The last line doesn't compile:
>
> tst.cpp:26: error: request for member f in z, which is of non-
> class type B ()(A (*)())
>
> Obviously, because the compiler thinks that z is a function.
>
> While it is easy to work arround the problem with an intermediate
> variable for class B, it annoys me because I need to instantiate a
> rather big amount of objects, an I'd like to do it in a table-like
> manner.
>
> Thus, my question: Is there a way to tell the compiler that z is an
> object while keeping the table-based initialization sceme?
>
> Thanks,
>
> Sam


Hi

It's about the Vexing phrase.
C++ has parsing ambiguity between a function declaration
and a constructor invocation when the constructor arguments
are temporaries. This is known as the "most vexing parse" problem.
Put the temporary object inside (). I mean
B z((A()));
For further information, please see:
http://www.informit.com/guides/conte...lus&seqNum=439
and
http://en.wikipedia.org/wiki/Most_vexing_parse

HTH,
-- Saeed Amrollahi
 
Reply With Quote
 
AdlerSam
Guest
Posts: n/a
 
      02-16-2011
> Put the temporary object inside (). I mean
> B z((A()));

Thanks a lot, the workaround is _exactly_ what I was looking for!

> This is known as the "most vexing parse" problem.

And while I new the problem before, it's nice being able to name it
now.

 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      02-17-2011
On Feb 16, 3:11 pm, AdlerSam <(E-Mail Removed)> wrote:

> please consider the following example:


> struct A {
> A(int) {}
> A() {}};


> struct B {
> B(const A &a): _a(a) {}
> B(int x, const A &a): _a(a) {}
> void f() {}
> A _a;};


> int main() {
> B v[] = {
> B(A(3)),
> B(27, A()),
> B(A())
> };
> v[0].f();
> v[1].f();
> v[2].f();


> B x(A(3));
> B y(27, A());
> B z(A());
> x.f();
> y.f();
> z.f();
> }


> The last line doesn't compile:


> tst.cpp:26: error: request for member f in z, which is of non-
> class type B ()(A (*)())


> Obviously, because the compiler thinks that z is a function.


That's because it's declared as a function.

> While it is easy to work arround the problem with an intermediate
> variable for class B, it annoys me because I need to instantiate a
> rather big amount of objects, an I'd like to do it in a table-like
> manner.


> Thus, my question: Is there a way to tell the compiler that z is an
> object while keeping the table-based initialization sceme?


Just add an extra pair of parentheses: B z((A()));
or use copy initialization: B z = B(A());

--
James Kanze
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
what's the difference between value-initialization and default-initialization? Jess C++ 23 05-04-2007 03:03 AM
array initialization in initialization list. toton C++ 5 09-28-2006 05:13 PM
Initialization of non-integral type in initialization list anongroupaccount@googlemail.com C++ 6 12-11-2005 09:51 PM
Initialization via ctor vs. initialization via assignment Matthias Kaeppler C++ 2 07-18-2005 04:25 PM
Default Initialization Vs. Value Initialization JKop C++ 10 09-22-2004 07:26 PM



Advertisments