0

This is similar to Template specialization with variadic templates, however, that was asked in 2011 and the answer was incomplete support from compiler.

Consider the following code

#include <iostream>

template< typename resource >
struct smart_ref{};

template< class resource, class linked, class... arg_types >
smart_ref< resource > make_smart( linked const& lnk, arg_types&&... args )
{
    return smart_ref< resource >{};
}

struct image
{
    struct linked {};
};

template<>
smart_ref< image > make_smart< image, image::linked, int const >( image::linked const& lnk, int const arg )
{
    return smart_ref< image >{};
}

int main()
{
   //auto img = make_smart< image >( image::linked{}, 2 ); 
   return 0;
}

Above code gives following error

$g++ -o main *.cpp
main.cpp:20:20: error: template-id ‘make_smart<image, image::linked, const int>’ for ‘smart_ref<image> make_smart(const image::linked&, int)’ does not match any template declaration
 smart_ref< image > make_smart< image, image::linked, int const >( image::linked const& lnk, int const arg )
                    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp:9:23: note: candidate is: template<class resource, class linked, class ... arg_types> smart_ref<resource> make_smart(const linked&, arg_types&& ...)
 smart_ref< resource > make_smart( linked const& lnk, arg_types&&... args )
                       ^~~~~~~~~~

In template specialization, I've tried with make_smart(...), make_smart< image >(...) and make_smart< image, image::linked > too, but it is still giving above error. What is the correct syntax for template specialization in this case?

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
Aneel
  • 71
  • 4

1 Answers1

0

specialization must exactly match the template declaration i.e. arg arguments must be int && arg rather than int const arg

Aneel
  • 71
  • 4