Connecting Tech Pros Worldwide Help | Site Map

Type of ios manipulators

Lionel B
Guest
 
Posts: n/a
#1: Apr 20 '07
The following code compiles ok and runs as expected:

#include <iostream>
#include <iomanip>

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

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::ostream& (*)(std::ostream&)’ 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 Traitsbasic_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?

--
Lionel B
Alf P. Steinbach
Guest
 
Posts: n/a
#2: Apr 20 '07

re: Type of ios manipulators


* Lionel B:
Quote:
The following code compiles ok and runs as expected:
>
#include <iostream>
#include <iomanip>
>
int main()
{
typedef std::ostream& (*manip_t)(std::ostream&);
>
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::ostream& (*)(std::ostream&)’ 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 Traitsbasic_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.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Lionel B
Guest
 
Posts: n/a
#3: Apr 20 '07

re: Type of ios manipulators


On Fri, 20 Apr 2007 13:03:09 +0200, Alf P. Steinbach wrote:
Quote:
* Lionel B:
Quote:
>The following code compiles ok and runs as expected:
>>
>#include <iostream>
>#include <iomanip>
>>
>int main()
>{
> typedef std::ostream& (*manip_t)(std::ostream&);
>>
> 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::ostream&
> (*)(std::ostream&)’ 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 Traitsbasic_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.
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,

--
Lionel B
James Kanze
Guest
 
Posts: n/a
#4: Apr 20 '07

re: Type of ios manipulators


On Apr 20, 12:56 pm, Lionel B <m...@privacy.netwrote:
Quote:
The following code compiles ok and runs as expected:
Quote:
#include <iostream>
#include <iomanip>
Quote:
int main()
{
typedef std::ostream& (*manip_t)(std::ostream&);
Quote:
manip_t x = std::endl;
Quote:
if (x==static_cast<manip_t>(std::endl))
std::cout << "endl detected" << std::endl;
return 0;
}
Quote:
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
Quote:
which I interpret as:
>
main.cpp:10: error: assuming cast to type ?std::ostream& (*)(std::ostream&)? from overloaded function
Quote:
ie.
Quote:
main.cpp:10: error: assuming cast to type ?manip_t? from overloaded function
Quote:
But my standard library ref defines:
Quote:
template<typename CharT, typename Traitsbasic_ostream<CharT, Traits>& endl(basic_ostream< CharT, Traits >&)
Quote:
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.
Quote:
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.

--
James Kanze (Gabi Software) email: james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Closed Thread