Skip to content

Instantly share code, notes, and snippets.

@Taraflex
Created December 23, 2015 05:35
Show Gist options
  • Save Taraflex/a57e3c59bd7c9c39ff19 to your computer and use it in GitHub Desktop.
Save Taraflex/a57e3c59bd7c9c39ff19 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <string>
#include <sstream>
#include "SSWrapper.h"
using namespace std;
int main()
{
string b{ "b" };
cout << 2 % 3 % b << endl;
std::string c = 'a' % b;
cout << c << endl;
cout << 'a' % b % 1 << endl;
wcout << 100500 % wstring{ L"Test wchar string" } << endl;
system("pause");
}
#pragma once
#include <iostream>
#include <sstream>
#include <string>
template<typename BasicCharType>
class SSWrapper
{
public:
static SSWrapper<BasicCharType>& instance()
{
static auto *inst = new SSWrapper<BasicCharType>();
return *inst;
}
template<typename T>
inline SSWrapper<BasicCharType>& add(T& val)
{
ss << val;
return *this;
}
inline operator std::basic_string<BasicCharType>()
{
auto ret = ss.str();
ss.str(std::basic_string<BasicCharType>());
return ret;
}
private:
SSWrapper() = default;
SSWrapper(const SSWrapper<BasicCharType>&) = delete;
SSWrapper& operator=(SSWrapper<BasicCharType>&) = delete;
std::basic_stringstream<BasicCharType> ss;
};
//=========================================================
template<typename CharType, typename T2>
inline SSWrapper<CharType>& operator%(std::basic_string<CharType> lft, T2 rgt)
{
return SSWrapper<CharType>::instance().add(lft).add(rgt);
}
template<typename T1, typename CharType>
inline SSWrapper<CharType>& operator%(T1 lft, std::basic_string<CharType> rgt)
{
return SSWrapper<CharType>::instance().add(lft).add(rgt);
}
//=========================================================
template<typename CharType, typename T2>
inline SSWrapper<CharType>& operator%(SSWrapper<CharType>& lft, T2 rgt)
{
return lft.add(rgt);
}
//=========================================================
template<typename CharType>
inline std::basic_ostream<CharType>& operator<<(std::basic_ostream<CharType>& os, SSWrapper<CharType>& wrp)
{
return os << std::basic_string<CharType>(wrp);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment