简体   繁体   中英

How to pass context information to a custom operator<< for std::iostream

I have a heavily used (and optimized) type which stores references to more data by using small integers. For debugging I would like to write an operator<< which access the additional data for creating a better debug output. How could I pass this additional context information to operator<< ?

Here is an example:

/** Rich information */
struct Item {
  short id;
  std::string name;
  ...
};    
struct Database {
  std::map<short,Item> items;
  ...
};

/** Very optimized light-weight data type */
struct Foo {
  short a, b; // actually references to Item
};

/** for debugging etc. */
void print(std::ostream& os, const Foo& f, const Database& db) {
  os << "(" << db.items[f.a].name << "," << db.items[f.b].name << ")";
}

Database db;
items[0] = {0, "item0"};
items[1] = {1, "item1"};
Foo foo{0,1};

std::cout << "foo="; print(std::cout, foo, db); std::cout << std::endl; // ugly
std::cout << "foo=" << foo << std::endl; // How to get db ???

You can create your own manipulator print_database which overloads operator << :

class print_database
{
public:
    print_database(const Foo& foo, const Database& db)
        : foo(foo), db(db)
    { }

    template<class cT>
    friend std::basic_ostream<cT>& operator<<(std::basic_ostream<cT>& os,
                                              const print_database& manip)
    {
        manip.print(os);
        return os;
    }
private:
    Foo foo;
    Database db;

    template<class cT>
    void print(std::basic_ostream<cT>& os) const
    {
        os << "(" << db.items[foo.a].name << "," << db.items[foo.b].name << ")";
    }
};

Then your print function can be implemented as:

print_database print(const Foo& foo, const Database& db)
{
    return print_database(foo, db);
}

So now you can call it as:

std::cout << print(foo, db) << std::endl;

I don't know if you consider it more or less ugly, but perhaps something like:

debug_printer dp { db };
std::cout << dp(foo) << std::endl;

Where the op<< is overload for debug printer; or op() could return a std::string .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM