C++ - How to introduce overload set from variadic number of bases.

后端 未结 1 1019
说谎
说谎 2021-01-04 03:26

The derived class hides the name of an overload set from the base class if the derived class has the same name defined, but we can always introduce that overload set back wi

相关标签:
1条回答
  • 2021-01-04 04:10

    Here is a trick how to reduce handwriting:

    // U<X,Y> is a binary operation on two classes
    template<template<class,class>class U, class... Xs> struct foldr;
    template<template<class,class>class U, class X> struct foldr<U,X> : X {};
    template<template<class,class>class U, class X, class... Xs> struct foldr<U,X,Xs...> : U<X, foldr<U,Xs...>> {};
    
    // our operation inherits from both classes and declares using the member f of them    
    template<class X, class Y> struct using_f : X,Y { using X::f; using Y::f; };
    
    struct A { void f(int) {} };
    struct B { void f(char) {} };
    struct C { void f(long) {} };
    
    struct D : foldr<using_f, A, B, C> {};
    
    
    int main() {
        D d;
        d.f(1);
        d.f('1');
        d.f(1L);
        return 0;
    }
    

    So we should write foldr once, then write simple ad-hoc operations - using_f, using_g, using_f_g

    Maybe there is a way to further simplifying. Let me think a bit...

    0 讨论(0)
提交回复
热议问题