Initializing a temporary with the return value of a function call

M

Marcel Müller

When compiling the following code I get the error below.
It seems that the call to WinQueryAccelTable is entirely ignored.

Is the code or the compiler wrong? (gcc.exe (GCC) 3.3.5 (Bird Build
2007-06-10 14:30))


----------
typedef void* HAB;
typedef unsigned long HWND;
typedef unsigned short HACCEL;

HACCEL WinQueryAccelTable(HAB hab, HWND hframe);

class MenuShowAccel
{public:
// Create worker class from accelerator table.
MenuShowAccel(HACCEL accel);
void ApplyTo(HWND menu, bool incl_sub = true);
};

HAB hab;
HWND hframe;
HWND hmenu;

void foo()
{ MenuShowAccel(WinQueryAccelTable(hab, hframe)).ApplyTo(hmenu);
}
----------

----------
D:\TEMP>gcc test.cpp
test.cpp: In function `void foo()':
test.cpp:19: error: no matching function for call to `MenuShowAccel::
MenuShowAccel(void*&, HWND&)'
test.cpp:8: error: candidates are: MenuShowAccel::MenuShowAccel(const
MenuShowAccel&)
test.cpp:10: error: MenuShowAccel::MenuShowAccel(short
unsigned int)
test.cpp:19: error: parse error before `.' token
----------
 
R

Ron Natalie

Marcel said:
void foo()
{ MenuShowAccel(WinQueryAccelTable(hab, hframe)).ApplyTo(hmenu);
}
----------

You're running afoul of the C++ rule that if it possibly could be
a declaration, to consider it such. It thinks you're defining
a function called WinQueryAccelTable that returns a MenuShowAccel.

If you give it either an explicit name or some extra parens it will
resolve the ambiguity in your favor.
 
M

Marcel Müller

Ron said:
You're running afoul of the C++ rule that if it possibly could be
a declaration, to consider it such. It thinks you're defining
a function called WinQueryAccelTable that returns a MenuShowAccel.

If you give it either an explicit name or some extra parens it will
resolve the ambiguity in your favor.

Thanks, I never would have considered this to be a declaration.


Marcel
 
T

thomas

class MenuShowAccel
{public:
   // Create worker class from accelerator table.
                 MenuShowAccel(HACCEL accel);
   void          ApplyTo(HWND menu, bool incl_sub = true);

{ MenuShowAccel(WinQueryAccelTable(hab, hframe)).ApplyTo(hmenu);} //A
why can the line A compile?
ApplyTo() has two parameters while provided only one?
 
B

Bo Persson

thomas said:
why can the line A compile?
ApplyTo() has two parameters while provided only one?

The second parameter has a default value ("= true"), which is applied
if you don't specify anything else.


Bo Persson
 
J

James Kanze

Marcel Müller wrote:
You're running afoul of the C++ rule that if it possibly could be
a declaration, to consider it such.

I think for the above to be a function, hab and hframe would
have to be the names of types, which they aren't. Also,
although I'm not sure, I think that there is enough context to
disambiguate---a function declaration cannot be followed by a
point (but I'm not sure whether look-ahead has to be considered,
and if so, how much).

FWIW: his code compiles for me (g++ 4.2.1 under Linux). His
error messages also seem to suggest that his compiler is also
treating MenuShowAccel as a function in the line in question.
In fact, it looks like the compiler is treating
WinQueryAccelTable as a macro which expands to its arguments
here, but if that were the case, the previous declaration would
not compile.
It thinks you're defining a function called WinQueryAccelTable
that returns a MenuShowAccel.

I don't think so. It shouldn't, and that doesn't correspond to
the error messages he posted either.
 
M

Marcel Müller

James said:
I don't think so. It shouldn't, and that doesn't correspond to
the error messages he posted either.

Whatever happend the additional braces around the expression fixed the
problem. So most likely gcc really thought that is was a declaration,
whether this is right or not.

Btw: my pretty old IBM compiler compiled it too. The error was specific
to the gcc port.


Marcel
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top