Generic implementation of operator<< function in terms of the dump member function

前端 未结 4 1869
时光说笑
时光说笑 2021-01-05 20:17

All my classes implement a dump member function, e.g.:

struct A {
    template 
    std::basic_ostream &
         


        
4条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-05 21:01

    Just added this for fun. In case you happen to have more than one method that prints/dumps on different classes:

    #include 
    #include 
    
    namespace tests {
    
        // this is a utility class to help us figure out whether a class
        // has a member function called dump that takes a reference to 
        // an ostream
        struct has_dump
        {
            // We will only be checking the TYPE of the returned
            // value of these functions, so there is no need (in fact we
            // *must not*) to provide a definition
            template
            static auto test(const T* t, std::basic_ostream& os)
            -> decltype(t->dump(os), std::true_type());
    
            // the comma operator in decltype works in the same way as the
            // comma operator everywhere else. It simply evaluates each
            // expression and returns the result of the last one
            // so if t->dump(os) is valid, the expression is equivalent to
            // decltype(std::true_type()) which is the type yielded by default-
            // constructing a true_type... which is true_type!
    
    
            // The above decltype will fail to compile if t->dump(os) is not
            // a valid expression. In this case, the compiler will fall back
            // to selecting this next function. this is because the overload
            // that takes a const T* is *more specific* than the one that
            // takes (...) [any arguments] so the compiler will prefer it
            // if it's well formed.
    
            // this one could be written like this:
            // template
            // static std::false_type test(...);
            // I just happen to use the same syntax as the first one to
            // show that they are related.
    
            template
            static auto test(...) -> decltype(std::false_type());
        };
    
        // ditto for T::print(ostream&) const    
        struct has_print
        {
            template
            static auto test(const T* t, std::basic_ostream& os)
            -> decltype(t->print(os), std::true_type());
    
            template
            static auto test(...) -> decltype(std::false_type());
        };
    }
    
    // constexpr means it's evaluated at compile time. This means we can
    // use the result in a template expansion.
    // depending on whether the expression t->dump(os) is well formed or not
    // (see above) it will either return a std::true_type::value (true!) 
    // or a std::false_type::value (false!)
    
    template
    static constexpr bool has_dump() {
        // the reinterpret cast stuff is so we can pass a reference without
        // actually constructing an object. remember we're being evaluated
        // during compile time, so we can't go creating ostream objects here - 
        // they don't have constexpr constructors.
        return decltype(tests::has_dump::test(nullptr,
                                                       *reinterpret_cast*>(0)))::value;
    }
    
    template
    static constexpr bool has_print() {
        return decltype(tests::has_print::test(nullptr,
                                                       *reinterpret_cast*>(0)))::value;
    }
    
    // so now we can use our constexpr functions has_dump<> and has_print<>
    // in a template expansion, because they are known at compile time.
    // std::enable_if_t will ensure that the template function is only
    // well formed if our condition is true, so we avoid duplicate
    // definitions.
    // the use of the alternative operator representations make this
    // a little more readable IMHO: http://en.cppreference.com/w/cpp/language/operator_alternative
    
    template
    auto operator<< (std::basic_ostream& os, const T& t)
    -> std::enable_if_t< has_dump() and not has_print(), std::basic_ostream&>
    {
        t.dump(os);
        return os;
    }
    
    template
    auto operator<< (std::basic_ostream& os, const T& t)
    -> std::enable_if_t< has_print() and not has_dump(), std::basic_ostream&>
    {
        t.print(os);
        return os;
    }
    
    template
    auto operator<< (std::basic_ostream& os, const T& t)
    -> std::enable_if_t< has_print() and has_dump(), std::basic_ostream&>
    {
        // because of the above test, this function is only compiled
        // if T has both dump(ostream&) and print(ostream&) defined.
    
        t.dump(os);
        os << ":";
        t.print(os);
        return os;
    }
    
    
    
    struct base
    {
        template
        void dump(std::basic_ostream& os) const
        {
            os << x;
        }
    
        int x = 5;
    };
    namespace animals
    {
        class donkey : public base
        {
    
        public:
            template
            void dump(std::basic_ostream& s) const {
                s << "donkey: ";
                base::dump(s);
            }
        };
    
        class horse // not dumpable, but is printable
        {
        public:
            template
            void print(std::basic_ostream& s) const {
                s << "horse";
            }
        };
    
        // this class provides both dump() and print()        
        class banana : public base
        {
        public:
    
            void dump(std::ostream& os) const {
                os << "banana!?!";
                base::dump(os);
            }
    
            void print(std::ostream& os) const {
                os << ":printed";
            }
    
        };
    }
    
    
    auto main() -> int
    {
        using namespace std;
    
        animals::donkey d;
        animals::horse h;
    
        cout << d << ", " << h << ", " << animals::banana() << endl;
    
        return 0;
    }
    

    expected output:

    donkey: 5, horse, banana!?!5::printed
    

提交回复
热议问题