std :: vector
我试图编写一个模板函数来处理任何类型的参数,包括向量,地图等,但我遇到了麻烦。std :: vector <T>
template<class C>
void foo(C c);
template<>
template<class V>
void foo<std::vector<V> >(std::vector<V> v);
编译器(G ++ 4.9.2-10)会抱怨这样的:
test.cpp:13:43: error: too many template parameter lists in declaration of ‘void foo(std::vector<V>)’
void foo<std::vector<V> >(std::vector<V> v) {
^
test.cpp:13:6: error: template-id ‘foo<std::vector<V> >’ for ‘void foo(std::vector<V>)’ does not match any template declaration
void foo<std::vector<V> >(std::vector<V> v) {
^
test.cpp:13:43: note: saw 2 ‘template<>’, need 1 for specializing a member function template
void foo<std::vector<V> >(std::vector<V> v) {
^
我怎样才能做到这一点? 我也想专注于的std ::地图<的std :: string,V >
从专业化去除第一模板< >线仍然无法正常工作(非法专业化)
你不能偏特一个功能,您要创建一个新的重载:
template<class C>
void foo(C c);
template<class V>
void foo(std::vector<V> v); // This is an overload, not a specialization!
请注意,使用的foo
部分专业化的区别:
template<class C>
void foo(C c); // 1
template<class V>
void foo<std::vector<V>>(std::vector<V> v); // 2
这里是2的部分特化,这在C++中是不允许的。
我试过了,它不起作用,我得到这个输出: test.cpp:12:43:error:function模板部分特例 '富>' 不允许 空隙FOO >(标准::矢量
@BogdanIonitza没有' >''''''foo'和我的代码中的开头括号之间 - 如果你添加它,你试图部分专门化一个函数,但你不能在C++中做到这一点,你需要重载它(就像我在我的代码中那样)。 – Holt
你是对的,我错过了那个方面。谢谢,它似乎工作:) –
根据容器的特性,您可能会发现方便过载。
这允许在接受引用,常量引用和r值引用方面稍微灵活一点,而只写一次函数。
这是它实现了一个模板foo
为任何具有begin()
和end()
方法,除了标准::字符串,它通过一个非模板超载都有自己的foo
版本的一个小例子:
#include <vector>
#include <map>
#include <iostream>
#include <iomanip>
// trait type to determine whether something models a range.
template<class T>
struct is_range
{
template <class Y> static auto has_begin(T*p) -> decltype(p->begin(), void(), std::true_type());
template <class Y> static auto has_begin(...) -> decltype(std::false_type());
template <class Y> static auto has_end(T*p) -> decltype(p->end(), void(), std::true_type());
template <class Y> static auto has_end(...) -> decltype(std::false_type());
static constexpr bool value = decltype(has_begin<T>(0))::value && decltype(has_end<T>(0))::value;
};
// specialised mini-functor for dealing with corner cases
template<class T>
struct emitter
{
std::ostream& operator()(std::ostream& os, const T& t) const {
return os << t;
}
};
template<class T, class U>
struct emitter<std::pair<T, U>>
{
std::ostream& operator()(std::ostream& os, const std::pair<T, U>& p) const
{
return os << "(" << p.first << ", " << p.second << ")";
}
};
// a version of foo which works for all known containers, whether temporararies or references
template<class Container,
std::enable_if_t<is_range<std::decay_t<Container>>::value and not std::is_same<std::decay_t<Container>, std::string>::value>* = nullptr
>
void foo(Container&& c)
{
// do things with c.begin(), c.end()
bool first = true;
for (auto& x : c) {
using emitter_type = emitter<std::decay_t<decltype(x)>>;
auto emit = emitter_type();
if (first) {
first = false;
} else {
std::cout << ", ";
}
emit(std::cout, x);
}
std::cout << std::endl;
}
// overload for std string
void foo(const std::string& s)
{
std::cout << std::quoted(s) << std::endl;
}
int main()
{
using namespace std::literals;
foo(std::map<std::string, std::string> {
{
{ { "foo" }, { "bar" } },
{ { "aaaa" }, { "bbbbb" } }
}
});
foo(std::vector<std::string> {
{ "foo" },
{ "bar" },
{ "xxxx" },
{ "yyyy" } });
foo("hello"s);
}
预期输出:
(aaaa, bbbbb), (foo, bar)
foo, bar, xxxx, yyyy
"hello"
只是有一个'模板'并使用它作为你的容器 –
[OT]:你可能想通过const引用,而不是用v来传递参数ALUE。 – Jarod42
@ Jarod42好点,我一直这样做,但为了简单起见,在这里忽略它 –