Bring templated base class conversion operator into derived scope

后端 未结 4 1766
旧巷少年郎
旧巷少年郎 2021-01-21 06:51

I have a base class that defines a constrained templated conversion operator

struct base {
    template 

        
4条回答
  •  有刺的猬
    2021-01-21 07:24

    Following trick requires GNU g++, with C++14.

    If some_constraints and different_constraints are mutually exclusive, you can add using base::operator auto; in derived class to make all the conversion operators of base available.

    Example:

    struct Base {
    protected:
        // conversion to pointer types (protected).
        template::value>::type>
        operator T() const;
    };
    
    struct Derived : public Base{
        // Make all Base conversion operators public.
        using Base::operator auto;
    
        // conversion to default constructible & non-pointer types.
        // added an extra typename=void to distinguish from Base template operator.
        template::value>::type,typename=void>
        operator T() const;
    };
    

    Live demo

提交回复
热议问题