473,473 Members | 1,733 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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
Apr 20 '07 #1
3 2739
* Lionel B:
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?
Apr 20 '07 #2
On Fri, 20 Apr 2007 13:03:09 +0200, Alf P. Steinbach wrote:
* Lionel B:
>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
Apr 20 '07 #3
On Apr 20, 12:56 pm, Lionel B <m...@privacy.netwrote:
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...
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.

--
James Kanze (Gabi Software) email: ja*********@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

Apr 20 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Andy | last post by:
1) Are the manipulators like std::endl and std::ends independent of character set. That is, can I use the same manipulators in both the following cases: a) <code> std::basic_stringstream<char>...
16
by: Darius.Moos AT esigma-systems DOT de | last post by:
Hi, there seems to be a problem with manipulators in g++/gcc-2.96. First some are not defined in std:: and second, when using this manipulators on streams, they give strange results. A small...
3
by: paul.wilkins | last post by:
Hi, I have a function I have written that works with g++ 2.95.3 that I would like to use with newer g++ compilers >= 3.2.0. The function is a stream manipulator similar to setw(). The code...
6
by: S.Tobias | last post by:
I'm trying to understand how structure type completion works. # A structure or union type of unknown # content (as described in 6.7.2.3) is an incomplete type. It # is ...
0
by: Chris Fink | last post by:
When I am consuming a webservice, an object has an undefined value (inq3Type.Call3Data). I do not completely understand why this is happening and apologize for the vague question. My assumption...
1
by: | last post by:
The code: // Block 1 // cout << "Integrating " << str << " on " << endl << " exact answer: " << exact << endl << flush; #if 0 // Block 2
1
by: sowmyth | last post by:
hello! can anybody plz tell me abt the different manipulators in c++ & what r their functions,how to use them,etc? ...
11
by: utab | last post by:
Dear all, I have a simple template to convert its argument to a string: template<typename T> string AtoStr(T t){ ostringstream sstrm; sstrm << t; return sstrm.str(); }
9
by: David W | last post by:
A colleague wants to do this with a std::ostream: os << fahrenheit << 37.0; This would output 98.6 (i.e., Celsius --Fahrenheit). I've already suggested os << fahrenheit(37.0), but he doesn't...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.