Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include <sstream>
- #include <initializer_list>
- namespace Internal
- {
- // In real code those are declared `extern` and initialization is moved from .h into .cpp
- std::stringstream ss;
- const std::stringstream::fmtflags stdfmt = ss.flags();
- }
- // Jo() resets stringstream flags before performing concatenation.
- // Jo_() does not.
- template <typename ...P> const char *Jo_(P &&... p)
- {
- // Multiple buffers to allow use of multiple Jo()'s in a single expression.
- static constexpr int ret_buffers_c = 32;
- static std::string ret_buffers[ret_buffers_c];
- static int ret_pos = 0;
- Internal::ss.clear(); // Clear the error flags.
- Internal::ss.str(""); // Clear stringstream contents.
- // Push all objects to stringstream.
- std::initializer_list<int>{(Internal::ss << p, 0)...};
- // Save to buffer and return a pointer.
- ret_buffers[ret_pos] = Internal::ss.str();
- const char *ret = ret_buffers[ret_pos].c_str();
- ret_pos = (ret_pos + 1) % ret_buffers_c;
- return ret;
- }
- template <typename ...P> const char *Jo(P &&... p)
- {
- Internal::ss.flags(Internal::stdfmt);
- return Jo_((P &&) p...);
- }
- // Example usage:
- int main()
- {
- for (int i = 1; i <= 10; i++)
- std::puts(Jo(i, " * ", i+1, " = ", i*i+i));
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement