简体   繁体   English

为什么可变参数模板 function 专业化无法与相关声明匹配

[英]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.

相关问题 可变参数模板功能的专业化 - Specialization of variadic template function VC ++ Variadic-Template-Template错误C2244:无法将函数定义与现有声明匹配 - VC++ Variadic-Template-Template error C2244: unable to match function definition to an existing declaration C ++,2参数类模板的部分特化:无法将函数定义与现有声明匹配 - C++, partial specialization of 2-argument class template: unable to match function definition to an existing declaration 可变参数模板函数的部分专业化 - Partial Specialization of a variadic template function 可变参数模板功能的模板专业化 - Template specialization in case of variadic template function 可变参数模板 function 模板 class 的专业化 - Variadic template function specialization in a template class 可变参数 function 模板中的模糊模板专业化 - ambiguous template specialization in variadic function template 模板特化:与任何模板声明都不匹配 - template specialization : does not match any template declaration C ++模板部分特化:为什么我不能匹配variadic-template中的最后一个类型? - C++ template partial specialization: Why cant I match the last type in variadic-template? MSVC的可变参数函数模板专业化的问题? - Bug in variadic function template specialization with MSVC?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM