c++ - stream operators where lhs is not a std::iostream instance -


i have custom output class has 2 std::ostream members serve different purposes. either stream used depending upon how output class configured. in instances, 2 streams chained together. grossly simplified version of class below. can provide more details if needed.

class c_output {   public:     c_output (bool x_usea) : m_usea(x_usea) { /* setup m_stream[ab] here */ };     ~c_output ();     inline std::ostream& stream () { return (m_usea ? m_streama : m_streamb); };    private:     bool m_usea;     std::ostream m_streama;     std::ostream m_streamb; } 

i know how write stream operators classes wish stream to/from std::cout, std::cin, or other std::iostream, struggling write stream operators c_output instance serves lhs instead of std::ostream instance.

right now, able away with:

c_output l_output; uint64_t l_value = 0xc001c0de; l_output.stream() << std::hex << std::setw(16) << std::setfill('0') << l_value; 

c_output::stream() returns appropriate std::ostream&, behaves expected.

i rewrite above as:

c_output l_output; uint64_t l_value = 0xc001c0de; l_output << std::hex << std::setw(16) << std::setfill('0') << l_value; 

i have attempted several different versions of defining operator<< based on examples have seen here on stackoverflow , greater web no avail. latest version looks this:

// in header class c_output {     ...     friend c_output& operator<< (c_output& x_output, std::ostream& x_stream);     ... }  // in source c_output& operator<< (c_output& x_output, std::ostream& x_stream) {     x_output.stream() << x_stream;     return x_output; } 

the setup of arguments intended mirror standard stream operator overload. setup gives me compile issues such as:

error: no match 'operator<<' in 'l_output << std::hex' note: candidates are: c_output& operator<<(c_output&, std::ostream&) 

i have stripped away file , line information, gets point across. getting type rhs of operator incorrect. correct type and/or correct means of implementing stream operator desired?

there complementary c_input class has similar requirement, adapting answer c_output should trivial.

the type of std::hex std::ios_base& (*)(std::ios_base&). change operator signature to:

c_output& operator<< (c_output& x_output, std::ios_base& (*x_stream)(std::ios_base&)); 

Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -