Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old April 20th, 2007, 12:05 PM
Lionel B
Guest
 
Posts: n/a
Default Type of ios manipulators

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
  #2  
Old April 20th, 2007, 12:05 PM
Alf P. Steinbach
Guest
 
Posts: n/a
Default 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?
  #3  
Old April 20th, 2007, 12:15 PM
Lionel B
Guest
 
Posts: n/a
Default 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
  #4  
Old April 20th, 2007, 10:35 PM
James Kanze
Guest
 
Posts: n/a
Default 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

 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles