table initialization

Discussion in 'C++' started by AdlerSam, Feb 16, 2011.

  1. AdlerSam

    AdlerSam Guest

    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
    AdlerSam, Feb 16, 2011
    #1
    1. Advertising

  2. On Feb 16, 6:11 pm, AdlerSam <> 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/content.aspx?g=cplusplus&seqNum=439
    and
    http://en.wikipedia.org/wiki/Most_vexing_parse

    HTH,
    -- Saeed Amrollahi
    Saeed Amrollahi, Feb 16, 2011
    #2
    1. Advertising

  3. On Feb 16, 6:11 pm, AdlerSam <> 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/content.aspx?g=cplusplus&seqNum=439
    and
    http://en.wikipedia.org/wiki/Most_vexing_parse

    HTH,
    -- Saeed Amrollahi
    Saeed Amrollahi, Feb 16, 2011
    #3
  4. AdlerSam

    AdlerSam Guest

    > 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.
    AdlerSam, Feb 16, 2011
    #4
  5. AdlerSam

    James Kanze Guest

    On Feb 16, 3:11 pm, AdlerSam <> 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
    James Kanze, Feb 17, 2011
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JKop
    Replies:
    10
    Views:
    926
  2. Matthias Kaeppler
    Replies:
    2
    Views:
    430
    Victor Bazarov
    Jul 18, 2005
  3. Replies:
    6
    Views:
    451
    Ron Natalie
    Dec 11, 2005
  4. toton
    Replies:
    5
    Views:
    920
    Victor Bazarov
    Sep 28, 2006
  5. Jess
    Replies:
    23
    Views:
    913
Loading...

Share This Page