B
Brendon Costa
Is the following valid C++?
I expected the output (This is correct with MSVC 6 or GCC when the
<<std::flush is in the code, but only works with MSVC when the
<<std::flush is omitted) from below to be:
Hello 3
C2
The incorrect output when compiled with GCC (without <<std::flush)
looks something like:
0x4410003
672
Note:
With the "<< std::flush" all seems to work fine with GCC (On both
linux + windows using MinGW) and also works fine on windows with MSVC
6.
Without the "<< std::flush" this works with MSVC 6 but with GCC it
will not output as i expect, but will output the address of the string
literal (As though it is printing a const void* instead of a const
char*).
I also checked this with a borland compiler and it does the same as
the GCC compiler. Which makes me think that there is a bug in the
code... I was thinking that the ostringstream temporary may not exist
throughout the duration of the function call but i dont think that is
the case anymore.
Is there another way to do what i am trying to achieve?
Thanks,
Brendon.
#include <iostream>
#include <sstream>
// NOTE: The flush is because for some reason passing a string literal
// as the first parameter seemed to display the string literals
address
// as though it was formatting a const void* not a const char* string
literal
#define STR(Message) \
static_cast<std:stringstream&>(std:stringstream() \
<< std::flush \
<< Message).str()
void F(const std::string& s)
{
std::cout << s << std::endl;
}
int main()
{
F(STR("Hello " << 3 << " again"));
F(STR('C' << 2));
return 0;
}
I expected the output (This is correct with MSVC 6 or GCC when the
<<std::flush is in the code, but only works with MSVC when the
<<std::flush is omitted) from below to be:
Hello 3
C2
The incorrect output when compiled with GCC (without <<std::flush)
looks something like:
0x4410003
672
Note:
With the "<< std::flush" all seems to work fine with GCC (On both
linux + windows using MinGW) and also works fine on windows with MSVC
6.
Without the "<< std::flush" this works with MSVC 6 but with GCC it
will not output as i expect, but will output the address of the string
literal (As though it is printing a const void* instead of a const
char*).
I also checked this with a borland compiler and it does the same as
the GCC compiler. Which makes me think that there is a bug in the
code... I was thinking that the ostringstream temporary may not exist
throughout the duration of the function call but i dont think that is
the case anymore.
Is there another way to do what i am trying to achieve?
Thanks,
Brendon.
#include <iostream>
#include <sstream>
// NOTE: The flush is because for some reason passing a string literal
// as the first parameter seemed to display the string literals
address
// as though it was formatting a const void* not a const char* string
literal
#define STR(Message) \
static_cast<std:stringstream&>(std:stringstream() \
<< std::flush \
<< Message).str()
void F(const std::string& s)
{
std::cout << s << std::endl;
}
int main()
{
F(STR("Hello " << 3 << " again"));
F(STR('C' << 2));
return 0;
}