Type of ios manipulators

L

Lionel B

The following code compiles ok and runs as expected:

#include <iostream>
#include <iomanip>

int main()
{
typedef std::eek:stream& (*manip_t)(std::eek:stream&);

manip_t x = std::endl;

if (x==static_cast<manip_t>(std::endl))
std::cout << "endl detected" << std::endl;

return 0;
}

However, without the cast I get a compiler error:

main.cpp:10: error: assuming cast to type ‘std::basic_ostream<char, std::char_traits<char> >& (*)(std::basic_ostream<char, std::char_traits<char> >&)’ from overloaded function

which I interpret as:

main.cpp:10: error: assuming cast to type ‘std::eek:stream& (*)(std::eek:stream&)’ from overloaded function

ie.

main.cpp:10: error: assuming cast to type ‘manip_t’ from overloaded function

But my standard library ref defines:

template<typename CharT, typename Traits> basic_ostream<CharT, Traits>& endl(basic_ostream< CharT, Traits >&)

which also boils down to my manip_t... so why does my compiler
seem to think an explicit cast is necessary?
 
A

Alf P. Steinbach

* Lionel B:
The following code compiles ok and runs as expected:

#include <iostream>
#include <iomanip>

int main()
{
typedef std::eek:stream& (*manip_t)(std::eek:stream&);

manip_t x = std::endl;

if (x==static_cast<manip_t>(std::endl))
std::cout << "endl detected" << std::endl;

return 0;
}

However, without the cast I get a compiler error:

main.cpp:10: error: assuming cast to type ‘std::basic_ostream<char, std::char_traits<char> >& (*)(std::basic_ostream<char, std::char_traits<char> >&)’ from overloaded function

which I interpret as:

main.cpp:10: error: assuming cast to type ‘std::eek:stream& (*)(std::eek:stream&)’ from overloaded function

ie.

main.cpp:10: error: assuming cast to type ‘manip_t’ from overloaded function

But my standard library ref defines:

template<typename CharT, typename Traits> basic_ostream<CharT, Traits>& endl(basic_ostream< CharT, Traits >&)

which also boils down to my manip_t... so why does my compiler
seem to think an explicit cast is necessary?

A cast resolves the ambiguity of which overload you mean.
 
L

Lionel B

* Lionel B:

A cast resolves the ambiguity of which overload you mean.

Ah, ok. Comeau online gives a clearer (to me) error message:

"ComeauTest.c", line 10: error: cannot determine which instance of
function
template "std::endl" is intended
if (x == std::endl)
^
Thanks,
 
J

James Kanze

The following code compiles ok and runs as expected:
#include <iostream>
#include <iomanip>
int main()
{
typedef std::eek:stream& (*manip_t)(std::eek:stream&);
manip_t x = std::endl;
if (x==static_cast<manip_t>(std::endl))
std::cout << "endl detected" << std::endl;
return 0;
}
However, without the cast I get a compiler error:

main.cpp:10: error: assuming cast to type ?std::basic_ostream<char, std::char_traits<char> >& (*)(std::basic_ostream<char, std::char_traits<char> >&)? from overloaded function
which I interpret as:

main.cpp:10: error: assuming cast to type ?std::eek:stream& (*)(std::eek:stream&)? from overloaded function

main.cpp:10: error: assuming cast to type ?manip_t? from overloaded function
But my standard library ref defines:
template<typename CharT, typename Traits> basic_ostream<CharT, Traits>& endl(basic_ostream< CharT, Traits >&)
which also boils down to my manip_t...

If that were true, then your code wouldn't be legal. I'll bet
your manip_t is a type, and not a template.
so why does my compiler seem to think an explicit cast is
necessary?

Because std::endl is a template, and it has to know which one to
instantiation.
 

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,057
Latest member
KetoBeezACVGummies

Latest Threads

Top