Variable declaration taken as a function pointer declaration

Discussion in 'C++' started by Bolin, Dec 2, 2005.

  1. Bolin

    Bolin Guest

    When compiling the following code:

    Code:
    #include <iostream>
    
    struct B {};
    
    struct A
    {
      A(B b1, B b2) {};
      void foo() { std::cout << "foo called" << std::endl; }
    };
    
    int main(int argc, char * argv[])
    {
      A a(B(), B());
      a.foo();
      return 0;
    }
    [\code]
    
    the compiler will interprete the first line of the main function as a
    function pointer declaration, and thus will fail at the next line. Does
    somebody know why this is so, and if there is an elegant way to solve
    this problem that does not involve temporary variables?
    
    B.
    Bolin, Dec 2, 2005
    #1
    1. Advertising

  2. Bolin wrote:
    > When compiling the following code:
    >
    >
    Code:
    > #include <iostream>
    > 
    > struct B {};
    > 
    > struct A
    > {
    >   A(B b1, B b2) {};[/color]
    ..                  ^^^
    Trailing semicolon is extraneous here.
    [color=blue]
    >   void foo() { std::cout << "foo called" << std::endl; }
    > };
    > 
    > int main(int argc, char * argv[])
    > {
    >   A a(B(), B());
    >   a.foo();
    >   return 0;
    > }
    > [\code]
    > 
    > the compiler will interprete the first line of the main function as a
    > function pointer declaration, and thus will fail at the next line. Does
    > somebody know why this is so, and if there is an elegant way to solve
    > this problem that does not involve temporary variables?[/color]
    
    It is so because the language designers had to make a decision and they
    picked the "If it looks like a declaration, it is a declaration" solution.
    
    You can work around it by adding an extra set of parentheses around the
    arguments:
    
       A a((B()), (B()));
    
    V
    Victor Bazarov, Dec 2, 2005
    #2
    1. Advertising

  3. Bolin

    Guest

    Bolin wrote:
    > When compiling the following code:
    >
    >
    Code:
    > #include <iostream>
    >
    > struct B {};
    >
    > struct A
    > {
    >   A(B b1, B b2) {};
    >   void foo() { std::cout << "foo called" << std::endl; }
    > };
    >
    > int main(int argc, char * argv[])
    > {
    >   A a(B(), B());
    >   a.foo();
    >   return 0;
    > }
    > [\code]
    >
    > the compiler will interprete the first line of the main function as a
    > function pointer declaration, and thus will fail at the next line. Does
    > somebody know why this is so, and if there is an elegant way to solve
    > this problem that does not involve temporary variables?[/color]
    
    It is so because your code could either be read as a function
    declaration or a variable definition and the rule to disambiguate this
    is "if it could be a function declaration, it is a function
    declaration". The solution is to change the line that defines a to
    
    A a((B()), (B()));
    
    Gavin Deane
    , Dec 2, 2005
    #3
  4. Bolin

    Bolin Guest

    Thanks for your answer and your solution -- the thing that surprised me
    is that a bare B() could be interpreted as a function pointer.

    B.
    Bolin, Dec 2, 2005
    #4
  5. Bolin

    Guest

    Bolin wrote:
    > Thanks for your answer and your solution -- the thing that surprised me
    > is that a bare B() could be interpreted as a function pointer.


    Take a look at

    http://www.gotw.ca/gotw/075.htm

    particularly the logic up to examples 2d and 2e.

    Gavin Deane
    , Dec 2, 2005
    #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. glen stark
    Replies:
    2
    Views:
    688
    Ron Natalie
    Oct 10, 2003
  2. Fraser Ross
    Replies:
    4
    Views:
    1,026
    Fraser Ross
    Aug 14, 2004
  3. iceColdFire
    Replies:
    9
    Views:
    501
    John Carson
    May 23, 2005
  4. Replies:
    11
    Views:
    923
    Jonathan Burd
    Jan 25, 2005
  5. mohi
    Replies:
    10
    Views:
    642
    mlimber
    Aug 22, 2008
Loading...

Share This Page