Ok, well then no not really.
Say you have this:
Code: Select all
struct Thing1
{
int a, b, c;
std::string name;
};
struct Thing2
{
Vec3f position, velocity;
Rectf bb;
};
How are you going to have one function take care of both of these types?
The best you can do is to define overloads for them.
Code: Select all
struct Thing1
{
int a, b, c;
std::string name;
friend std::wcstream& operator<<( std::wcstream& wcs, Thing1 const& thing )
{
wcs << thing.name << " has these values "<< "a = " << thing.a << "b = " << thing.b << "c = " << thing.c << '\n';
return wcs;
}
};
struct Thing2
{
Vec3f position, velocity;
Rectf bb;
friend std::wcstream& operator<<( std::wcstream& wcs, Thing2 const& thing )
{
wcs << "Position: x = " << thing.position.x
<< ", y = " << thing.position.y
<< ", z = " << thing.position.z
<< "\nVelocity: x = " << thing.velocity.x
<< ", y = " << thing.velocity.y
<< ", z = " << thing.velocity.z
<< "\nBoundingBox: left = " << thing.bb.left
<< " top = " << thing.bb.top
<< " right = " << thing.bb.right
<< " bottom = " << thing.bb.bottom << '\n';
return wcs;
}
};
Well, ok, there might be one way to have a single function.
Make each user-defined type ( structs/classes ) have a function that returns a string or wstring of it's members or whatever you want.
Code: Select all
struct Thing1
{
std::string to_string()const
{
std::stringstream ss;
ss << thing.name << " has these values "<< "a = " << thing.a << "b = " << thing.b << "c = " << thing.c << '\n';
return ss.str();
}
int a, b, c;
std::string name;
};
struct Thing2
{
std::string to_string()const
{
std::stringstream ss;
ss << "Position: x = " << thing.position.x
<< ", y = " << thing.position.y
<< ", z = " << thing.position.z
<< "\nVelocity: x = " << thing.velocity.x
<< ", y = " << thing.velocity.y
<< ", z = " << thing.velocity.z
<< "\nBoundingBox: left = " << thing.bb.left
<< " top = " << thing.bb.top
<< " right = " << thing.bb.right
<< " bottom = " << thing.bb.bottom << '\n';
return ss.str();
}
Vec3f position, velocity;
Rectf bb;
};
template<typename T>
std::wcstream& operator<<( std::wcstream& wcs, T const& value )
{
wcs << value.to_string();
return wcs;
}
This will end up calling your operator<<(std::wcstream&, std::string) overload.
If you don't want to overload for each type, then define a common function for each type that the single template can call and use.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com