Type of endl, ends, flush in g++-3

V

Victor Irzak

Hello,

I have a g++-3.3.1 compiler (latest).
I can't figure the type of endl, ends, flush.

Here is my program.
It compiles with VS.NET, Intel C++ compiler, but not with g++.

If you can compile it using some other type, please post the type used.

TIA,

Victor Irzak


#include <iostream>

using namespace::std;

static void fn (

#ifdef __ICC
ostream &( *f )( ostream & )
#endif

#ifdef _WIN32
ostream &( *f )( ostream & )
#endif



#ifdef __GNUC__

#if 0
// Doesn't work
ostream &( *f )( ostream & )
#endif

#if 0
// Doesn't work
std::basic_ostream<char, std::char_traits<char> >&
(*f)(std::basic_ostream<char, std::char_traits<char> >&)
#endif

//////////// What should be here????????

#endif

) {
if (f == endl)
cout << "endl" << endl;
else if (f == flush)
cout << "flush" << endl;
else if (f == ends)
cout << "ends" << endl;
else
cout << "unknown" << endl;
}

int main() {
fn(ends);
fn(endl);
fn(flush);
return 0;
}
 
T

tom_usenet

Hello,

I have a g++-3.3.1 compiler (latest).
I can't figure the type of endl, ends, flush.

They are templates - they don't have a type.
Here is my program.
It compiles with VS.NET, Intel C++ compiler, but not with g++.

If you can compile it using some other type, please post the type used.

This is a standard compliant version (that should work on all standard
compilers)

#include <iostream>
#include <ostream>

using namespace std;

typedef ostream& (*manip_t)(ostream&);

static void fn(manip_t f) {
manip_t const endlp = endl;
manip_t const flushp = flush;
manip_t const endsp = ends;
if (f == endlp)
cout << "endl" << endl;
else if (f == flushp)
cout << "flush" << endl;
else if (f == endsp)
cout << "ends" << endl;
else
cout << "unknown" << endl;
}

int main() {
fn(ends);
fn(endl);
fn(flush);
return 0;
}


The problem is that endl, etc. are templates, and you need a "target
type" for them when you take their address, or the instantiation you
want can't be chosen. In the expression (f == endl) the compiler
doesn't know which endl instantiation to choose, since there is no
explicit "target type". In the following cases you have an explicit
target type:

ostream& (*endlp)(ostream&) = endl; //target type from lhs
static_cast<ostream&(*)(ostream&)>(endl);//target type is cast-to-type

The relevent parts of the standard are 13.4 and 14.8.2.2

Tom
 

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

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top