[英]why is variadic template function specialization unable to match with relevant declaration
This is similar to Template specialization with variadic templates , however, that was asked in 2011 and the answer was incomplete support from compiler.这类似于Template specialization with variadic templates ,然而,这是在 2011 年提出的,答案是编译器的不完全支持。
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.在模板专业化中,我也尝试过
make_smart(...)
、 make_smart< image >(...)
和make_smart< image, image::linked >
,但仍然出现上述错误。 What is the correct syntax for template specialization in this case?在这种情况下,模板专业化的正确语法是什么?
specialization must exactly match the template declaration ie arg
arguments must be int && arg
rather than int const arg
特化必须与模板声明完全匹配,即
arg
arguments 必须是int && arg
而不是int const arg
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.