Last active
December 23, 2015 05:34
-
-
Save AtomKrieg/c276e021e5220eda1e75 to your computer and use it in GitHub Desktop.
string concatenate operator with stringstream
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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