Search code examples
c++template-argument-deduction

Class template argument deduction for class with additional non-type arguments


Is it possible to make CTAD work for T in this case?

enum class boundary_type:int{inclusive, exclusive};

template<class T, boundary_type left, boundary_type right>
struct interval
{
    using value_type = T;

    T begin;
    T end;
};

I tried to add the decuction guide


template<boundary_type left, boundary_type right, class T>
interval(T, T) -> interval<T, left, right>;

But still gets error

wrong number of template arguments (2, should be 3)

when trying

interval<boundary_type::inclusive, boundary_type::exclusive>{0, 2}

Solution

  • CTAD cannot work with the partially explicitly specified template parameter. But you can do this using normal template function

    template<boundary_type left, boundary_type right, class T>
    auto make_interval(T begin, T end) {
      return interval<T, left, right>{begin, end};
    }
    
    int main() {
      auto intval = make_interval<boundary_type::inclusive, 
                                  boundary_type::exclusive>(0, 2);
    }