类模板函数特化(专用化)specialization of …… after instantiation

本文介绍C++中模板函数特化的方法,并通过具体示例展示如何对不同类型的向量进行特化处理。文中提供了特化的两种解决方案:一是调整特化函数的位置;二是声明特化函数原型。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



c++ - Explicit specialization after instantiation - Stack Overflow

对模板特化的理解_利用类模板实现比较两个数的大小,再用类模板的特化实现比较字符串-优快云博客

typedef vector<int> Vec;
typedef vector<Vec> VecOfVec;

template<typename Vec>
Vec DoSomething(const Vec &v);

template<>
VecOfVec DoSomething<VecOfVec>(const VecOfVec &v)
{
    VecOfVec r;
    for(auto i = v.begin(); i != v.end(); i++)
        r.push_back(DoSomething(*i));
    return r;
}

template<>
Vec DoSomething<Vec>(const Vec &v) specialization of …… after instantiation
{
    return v; // for the sake of the example
}

VC2012编译正常,安卓NDK r10d编译出错“specialization of …… after instantiation”

解决方法:

1、把DoSomething特化放在出现调用之前

template<>
Vec DoSomething<Vec>(const Vec &v) 
{
    return v; // for the sake of the example
}

template<>
VecOfVec DoSomething<VecOfVec>(const VecOfVec &v)
{
    VecOfVec r;
    for(auto i

2、在头文件中声明特化(专用化)函数原型,以分号结尾:

template<>Vec DoSomething<Vec>(const Vec &v);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值