gpt4 book ai didi

c++ - 具有模板类的函数模板特化

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:24:19 25 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
partial specialization of function template

我无法在任何地方找到我的问题的解决方案,因为如果我使用我想出的关键字进行搜索,将会为我提供适合不同问题的解决方案。我知道这一定是以前问过的,只是找不到解决方案。

假设我有一个函数模板:

template<class any> print(any value);

我可以像这样专门化它,比如说 int:

template<> print<int>(int value)
{
std::cout << value;
}

但现在的问题是,我希望它也能与 vector 一起使用。由于 vector 类是模板类,因此变得困难。

像这样专门化函数:

template<class any> print<vector<any> >(vector<any> value) {}

将产生以下错误(MinGW g++):

FILE: error: function template partial specialization 'print<vector<any> >' is not allowed

请注意,函数 print 只是一个示例。

我该如何解决这个问题?

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com