1 year ago

#250057

test-img

joaocandre

Additional template parameter when specialiazing templated class function?

Is it possible to somehow add an extra template parameter when specializing a class template member?

For instance:

#include <iostream>
#include <utility>
#include <vector>

template < typename T >
class myClass {
 public:
    // ...
    template < typename... Args >
    void func(Args&&... args);
    // ...
    std::vector< T > vec;
}

template < typename T >
template < typename... Args >
void myClass< T >::func(Args&&... args) {
    vec.emplace_back(std::forward< Args >(args)...);
}

It is straightforward to specialize func for numerical types i.e.:

template < >
template < typename... Args >
void myClass< float >::func(Args&&... args) {
    std::cout << "myClass< float >::" << __func__ << std::endl;
    // ...
}

However, for more complex cases where templated code needs to be called from within the specialization:

class myOtherClass {
 public:
    // ...
    template< typename U >
    static void other_func();
}

template < >
template < typename... Args >
void myClass< myOtherClass >::func(Args&&... args) {
    std::cout << "myClass< myOtherClass >::" << __func__ << std::endl;
    // ...
    myOtherClass::other< /*???*/ >();
}

Is it possible to encode an additional template argument to be passed to the specialization? Changing any of the template-parameter-lists leads to the compiler not recognizing the template-id ([...] does not match any template declaration).

c++

templates

template-specialization

0 Answers

Your Answer

Accepted video resources