c++ - How can I call a member function via a stream insertion operator? -
Using GCC 4.8 with C ++ 11 is enabled, I have a class like this:
class outstream {public: outstream & amp; Operator & lt; & Lt; (Const char * s); Outstream & amp; Operator & lt; & Lt; (Int n); Outstream & amp; Operator & lt; & Lt; (Unsigned int n); // ... Outstream & amp; Vformat (const char * fmt, __VALIST args); Outstream & amp; Format (const char * fmt, ...); };
When I use this class by calling operators directly, then it works as I expected:
Outstream Out; Out.operator & lt; & Amp; Lt; (1). Format ("Formatted% 4X", 2). Assistant. & Lt; & Lt; ("3 \ n");
Output:
1 formatted 0002 3
Now, I want to get the same output but < Code> & lt; & Lt; By using streaming notation, maybe this way:
Outstream Out; Outside & lt; & Lt; 1 & lt; & Lt; Format ("Formatted% 04X", 2) & lt; & Lt; "3 \ n";
Of course, this will not compile, because there was no such operator for my Outstream.format ()
method streaming.
There may be a solution where format ()
was a free function that gives a string, but it should first be used in all buffers of format ()
Need to write requires a std :: string
or some other pile or buffer usage without any solutions - a solution that creates the same code when calling directly to the operators.
Any suggestions
Edit, 2014-10-20:
- To better understand my needs: I use the GCC cross toolchain I'm on bare metal embedded development.
- I need to apply the solution to some different embedded target systems (most Cortex-M0 / M3 / M4). Some of them are very limited resources (RAM and Flash) and one part of my target system should be run without using any heaps.
- For some reasons, I have
Stl
iostream
. However, the edit has been made by editing theiostream
tag; I keep it set due to the thematic match and can get a solution to my problem which can also be applied toSTL
iostream .
Use of C ++ 14 (contains):
template & lt; Typename ... Ts & gt; Class formatters {cst. * Fmt_; Std :: Tuple & LT; T ... & gt; Args_; Template & lt; Std :: size_t ... is & gt; Zero Extensions (Outstream and OS, STD :: Index_sense is <... & gt;) & amp; Amp; {Os.format (fmt_, std :: get & lt; is> (std :: move (args _)) ...); } Public: Template & lt; Typename ... Args & gt; Formatter (const char * fmt, args & args): fmt_ {fmt}, args_ {std :: forward & lt; ARGS & gt; (Args) ...} {} Friend Outstream & amp; Operator & lt; & Lt; (Outstream and OS, Formatter & amp; F) {std :: move (f). Expand (OS, std :: index_sequence_for & lt; ts ... & gt; {}); Return os; }}; Template & lt; Typename ... Args & gt; Formatter & LT; Args & amp; & Amp; ... & gt; Format (const char * fmt, erg and amp ... ... args) {returns {FMT, STD :: Forward & LT; ARGS & gt; (RGS) ...}; } The object must be removed. Actually this function: zero test_foo () {Outstream Out; Outside & lt; & Lt; 1 & lt; & Lt; Format ("Formatted% 04X", 2) & lt; & Lt; "3 \ n"; }
:
. LC0: .string "% 4X formatted". LC1: .String "3 \ n" test_foo (): pushq% rbx $ 1,% asi lesson $ 16,% rp leik 15 (% rsp),% rdi call outstream :: operator & lt; & Lt; MLL $ 2,% ADX MLL $ .cc,% seimpm% rx,% rbx mmpc% racks,% rdi xorl% eax,% eax call outstream :: format (thief cons *, ...) movq% rbx,% rdi Movl $. Lc1,% esi Call Outstream :: Operator & lt; & Lt; (Char const *) Addq $ 16,% rsp popq% rbx ret
So everything is properly underlined; There is no trace of formatter
in the code generated.
Comments
Post a Comment