Bring templated base class conversion operator into derived scope

后端 未结 4 1824
旧巷少年郎
旧巷少年郎 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

    To achieve a similar effect, you could move the constraints inside the operator and call the base conversion operator if the constraints are not satisfied:

    struct base {
        template , int> = 0>
        operator C() const;
    };
    
    struct derived : base {
        template 
        operator P() const {
            if constexpr (different_constraints

    ) { // Overridden operator } else { return base::operator P(); } } };

    Or equivalently have one regular std::enable_if_t, int> = 0 conversion operator and another std::enable_if_t, int> = 0 that calls the base conversion operator.

提交回复
热议问题