首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么可变模板函数专门化无法与相关声明匹配

为什么可变模板函数专门化无法与相关声明匹配
EN

Stack Overflow用户
提问于 2021-05-16 15:59:49
回答 1查看 19关注 0票数 0

然而,这与2011年提出的Template specialization with variadic templates类似,得到的答案是编译器的不完全支持。

考虑以下代码

代码语言:javascript
复制
#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;
}

上面的代码给出了以下错误

代码语言:javascript
复制
$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 )
                       ^~~~~~~~~~

在模板专门化方面,我也尝试过make_smart(...)make_smart< image >(...)make_smart< image, image::linked >,但它仍然给出上述错误。在这种情况下,模板专门化的正确语法是什么?

EN

回答 1

Stack Overflow用户

发布于 2021-05-16 19:58:50

专门化必须与模板声明完全匹配,即arg参数必须为int && arg而不是int const arg

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67554307

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档