Skip to content

Instantly share code, notes, and snippets.

@AtomKrieg
Last active December 23, 2015 05:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AtomKrieg/c276e021e5220eda1e75 to your computer and use it in GitHub Desktop.
Save AtomKrieg/c276e021e5220eda1e75 to your computer and use it in GitHub Desktop.
string concatenate operator with stringstream
#include <iostream>
#include <sstream>
#include <string>
class SSWrapper
{
public:
static SSWrapper& instance()
{
if(!inst)
inst = new SSWrapper;
return *inst;
}
template<typename T>
SSWrapper& add(T& val)
{
ss << val;
return *this;
}
operator std::string()
{
auto ret = ss.str();
ss.str("");
return ret;
}
private:
static SSWrapper *inst;
SSWrapper() {}
SSWrapper( const SSWrapper& );
SSWrapper& operator=( SSWrapper& );
std::stringstream ss;
};
template<typename T1, typename T2>
SSWrapper& operator%(T1 lft, T2 rgt)
{
return SSWrapper::instance().add(lft).add(rgt);
}
template<typename T2>
SSWrapper& operator%(SSWrapper& lft, T2 rgt)
{
return lft.add(rgt);
}
std::ostream& operator<<(std::ostream& os, SSWrapper& wrp)
{
return os << std::string(wrp);
}
#include <iostream>
#include <string>
#include <sstream>
#include "inc.h"
SSWrapper* SSWrapper::inst=nullptr;
using namespace std;
int main()
{
string b{"b"};
std::string c = 'a' % b;
cout << c << endl;
cout << 'a' % b % 1 << endl;
system("pause");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment