C++ and "flush" -- So very confused

E

Evan

So I have two versions of a program (these are complete programs, not
excerpts):

Version 1:
template <class T >
void foo() {
return bar( T() );
}


Version 2:
template <class T >
void foo() {
return (bar)( T() );
}


The only difference between the two is that in the second version, I
have put parentheses around bar.

GCC versions 3.4 and 4.1 and Comeau's online front end accept version 1
but reject version 2
ICC (9.1 I think), MSVC 7.1, and Sun CC accept both versions

So have a few questions:

1. There is no definition of bar. Why does it even compile at all?
(This holds true if I replace "flush" with something like "f") I guess
it's because it doesn't try to resolve the call until foo is
instantiated. Is this right?
2. Why is there a difference between the versions on GCC and Comeau?
3. What does the standard say about this? I'm guessing by #2 that it's
undefined

Evan
 
E

Evan

Evan said:
So I have two versions of a program (these are complete programs, not
excerpts):

Oops, ignore the title's reference to "flush".

Originally I had a bit more complex example that was minimized from a
preprocessed file, so had names left over from the actual headers.
("foo" used to be "endl" and "bar" used to be "flush".) However, I then
figured out more about what was happening, and that it didn't depend on
the "flush" name (which for a while I was wondering if it was a
compiler builtin or something), and I figured out another way to
simplify it further, and at that point I changed it from the minimized
example to something more abstract.

But I forgot to change the title. (The "so very confused" part is even
not nearly as strong as I thought; for a while I was very confused
about why the examples compiled at all, but then I realized it must not
do name lookup on bar/flush until instantiation, and things became a
lot clearer. So now I'm more posting to ask if MSVC, ICC, and Sun are
too permissive; GCC and Comeau too restrictive (with Comeau in there --
ha!); or if the standard leaves the version 2 case undefined.

Evan
 
B

Bo Yang

Evan :
So I have two versions of a program (these are complete programs, not
excerpts):

Version 1:
template <class T >
void foo() {
return bar( T() );
}


Version 2:
template <class T >
void foo() {
return (bar)( T() );
}


The only difference between the two is that in the second version, I
have put parentheses around bar.

GCC versions 3.4 and 4.1 and Comeau's online front end accept version 1
but reject version 2
ICC (9.1 I think), MSVC 7.1, and Sun CC accept both versions

So have a few questions:

1. There is no definition of bar. Why does it even compile at all?
(This holds true if I replace "flush" with something like "f") I guess
it's because it doesn't try to resolve the call until foo is
instantiated. Is this right?
2. Why is there a difference between the versions on GCC and Comeau?
3. What does the standard say about this? I'm guessing by #2 that it's
undefined

Evan
The function template will be initialized when it is used.
So the complier say nothing about the undifined name bar.
 
Z

Zara

So I have two versions of a program (these are complete programs, not
excerpts):

Version 1:
template <class T >
void foo() {
return bar( T() );

-- when the template is instantiated, it will look up for
something called bar, applicable in that case (i.e. it may be applied
to an object of type T, and will return void)
}


Version 2:
template <class T >
void foo() {
return (bar)( T() );
-- you are trying to make an old-styled cast of a T object
(just created) to something of type bar, and return it, with a
return-specification of void ==> error
}


The only difference between the two is that in the second version, I
have put parentheses around bar.

GCC versions 3.4 and 4.1 and Comeau's online front end accept version 1
but reject version 2
ICC (9.1 I think), MSVC 7.1, and Sun CC accept both versions

So have a few questions:

1. There is no definition of bar. Why does it even compile at all?
(This holds true if I replace "flush" with something like "f") I guess
it's because it doesn't try to resolve the call until foo is
instantiated. Is this right? Yes

2. Why is there a difference between the versions on GCC and Comeau?
You have the explanation above
3. What does the standard say about this? I'm guessing by #2 that it's
undefined
Without reading directly the standard, but applying the explanation
given above, you will see clearly tat Version 2 is wrong, and veriosn
1 is OK. When in doubt, trust Comeau.

Best regards,

Zara
 

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,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top