Ambiguous Function Calls to C++ base classes

前端 未结 3 406
旧巷少年郎
旧巷少年郎 2021-02-05 11:53

I\'m trying to create a variadic templated class which provides a method for each class in the typelist. An example is shown below which creates a print method for

3条回答
  •  深忆病人
    2021-02-05 12:30

    I don't like answering my own question but I've created the following solution from the comments here. It brings all print functions into scope and allows for C++ overload resolution on all of the functions.

    #include 
    #include 
    
    // Helper class providing a function call
    template 
    class PrintHelper
    {
    public:
        void print(const T& t) { std::cout << t << std::endl; }
    };
    
    // Provides a print method for each type listed
    template 
    class Printer
    {};
    
    template
    class Printer : public PrintHelper
    {
    public:
        using PrintHelper::print;
    };
    
    template
    class Printer: public PrintHelper, public Printer
    {
    public:
        using PrintHelper::print;
        using Printer::print;
    };
    
    int main()
    {
        Printer p;
        p.print("Hello World"); // Not an ambiguous Call
    }
    

提交回复
热议问题