Connecting Tech Pros Worldwide Help | Site Map

Variable declaration taken as a function pointer declaration

 
LinkBack Thread Tools Search this Thread
  #1  
Old December 2nd, 2005, 03:05 PM
Bolin
Guest
 
Posts: n/a
Default Variable declaration taken as a function pointer declaration

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.


  #2  
Old December 2nd, 2005, 03:35 PM
Victor Bazarov
Guest
 
Posts: n/a
Default Re: Variable declaration taken as a function pointer declaration

Bolin wrote:[color=blue]
> 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
  #3  
Old December 2nd, 2005, 03:35 PM
deane_gavin@hotmail.com
Guest
 
Posts: n/a
Default Re: Variable declaration taken as a function pointer declaration


Bolin wrote:[color=blue]
> 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

  #4  
Old December 2nd, 2005, 03:55 PM
Bolin
Guest
 
Posts: n/a
Default Re: Variable declaration taken as a function pointer declaration

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.

  #5  
Old December 2nd, 2005, 04:35 PM
deane_gavin@hotmail.com
Guest
 
Posts: n/a
Default Re: Variable declaration taken as a function pointer declaration


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

Take a look at

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

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

Gavin Deane

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.