Search code examples
c++templatesc++17typenamenon-type

Forward declare typename in template parameter list


I have a function with the following signature:

template<typename Container, auto First = 0, auto Last = Container::size()>
doSomething(const Container& containter){...}

Is there a way which will allow the reordering of the template parameters, so I would be able to call the function like this:

doSomething<3,5>(someContainer);

Rather than have to do this:

doSomething<decltype(someContainer), 3,5>(someContainer);

This wouldn't be an issue, if I could move someCountainer after Last, however the default value of Last is derived from Container. Is there a way to forward declare Container within the template parameter list, or any functionality that would actually allow me to avoid having to add decltype(...) every time?


Solution

  • One way is overloading:

    template<auto First, auto Last, typename Container>
    void doSomething(const Container& containter);
    
    template<auto First, typename Container>
    void doSomething(const Container& containter) {
        doSomething<First, Container::size()>(containter);
    }
    
    template<typename Container>
    void doSomething(const Container& containter) {
        doSomething<0, Container::size()>(containter);
    }