Policy class design but without making the whole user class a template

后端 未结 3 360
悲哀的现实
悲哀的现实 2021-01-15 05:28

Consider the following code where the Writer_I acts as an interface. Other classes which fulfil the contract of writing element types in correct form can derive

3条回答
  •  星月不相逢
    2021-01-15 05:45

    here are most of the errors gone except for line 41 #include #include

    enum class Elem {
    HEADER,
    FOOTER,
    };
    
    template  class Writer_I {
    public:
    template  decltype(auto) write(T &&...args) {
        return static_cast(this)->template write(
            std::forward(args)...);
    }
    virtual ~Writer_I() {}
    };
    
    class Streams : public Writer_I {
    public:
    template >
    void write(int a) {
        std::cout << a << std::endl;
    }
    template >
    void write(float a) {
        std::cout << "\n-------\n" << a << std::endl;
    }
    };
    
    /* Restrictions being that member functions header and footer
    remain in cpp files. And creators of Calculator
    can specify alternative implementations. */
    class Calculator {
    std::_Any_tag writer;
    
    public:
    template 
    Calculator(Writer_I& writer) : writer(writer) {}
    
    template  void write_i(T &&...args) {
        /*  */ writer(std::forward(args)); 
    }
    
    void header() {
        for (int i = 0; i < 10; i++) {
            write_i(i);
        }
    }
    
    void footer() {
        write_i(-100.0f);
    }
    };
    int main() {
    Streams streams;
    Calculator calc(streams);
    calc.header();
    return 0;
    }
    

提交回复
热议问题