A
Adam Nielsen
Hi everyone,
Following advice previously given in this group, I've created a function
that I'm using to "inline" the creation of a string. Regardless of what
you think of my method, I'm confused as to why the operator <<
overloading seems to be selecting the wrong function.
If you look at the example program below, it should print this:
The string is 'Five == 5'
However the first variable in the createString() parameter is always
treated as an integer or pointer, instead of its native type - hence you
end up with something like this:
The string is '0x804b485 == 5'
Could someone please enlighten me as to what's causing this behaviour?
Even if I pass "std::ios::dec" as the first parameter, this results in a
"2" being printed, which I find very unusual.
Thanks,
Adam.
#include <sstream>
#include <stdio.h>
inline std::string _createString(const std::basic_ostream<char,
std::char_traits<char> > &sstr)
throw ()
{
return static_cast<const std:stringstream&>(sstr).str();
}
#define createString(a) _createString(std:stringstream() << a)
int main(void)
{
std::string strTest = createString("Five" << " == " << 5);
printf("The string is '%s'\n", strTest.c_str());
return 0;
}
Following advice previously given in this group, I've created a function
that I'm using to "inline" the creation of a string. Regardless of what
you think of my method, I'm confused as to why the operator <<
overloading seems to be selecting the wrong function.
If you look at the example program below, it should print this:
The string is 'Five == 5'
However the first variable in the createString() parameter is always
treated as an integer or pointer, instead of its native type - hence you
end up with something like this:
The string is '0x804b485 == 5'
Could someone please enlighten me as to what's causing this behaviour?
Even if I pass "std::ios::dec" as the first parameter, this results in a
"2" being printed, which I find very unusual.
Thanks,
Adam.
#include <sstream>
#include <stdio.h>
inline std::string _createString(const std::basic_ostream<char,
std::char_traits<char> > &sstr)
throw ()
{
return static_cast<const std:stringstream&>(sstr).str();
}
#define createString(a) _createString(std:stringstream() << a)
int main(void)
{
std::string strTest = createString("Five" << " == " << 5);
printf("The string is '%s'\n", strTest.c_str());
return 0;
}