【发布时间】:2018-06-18 19:10:39
【问题描述】:
这似乎是一个标准案例:
#include <iostream>
#include <vector>
#include <utility>
#include <tuple>
using namespace std;
template <typename... T>
using VType = vector<tuple<T...>>;
template <typename... T>
void Foo(const T&... t, VType<T...>* v) {
v->push_back(std::make_tuple(t...));
}
int main() {
// your code goes here
VType<string, string> foo;
Foo(string("asdf"), string("qwerty"), &foo);
return 0;
}
如果你明确告诉编译器 Foo<string, string> 它工作正常,它无法推断:
error: no matching function for call to ‘Foo(std::__cxx11::string, std::__cxx11::string, VType<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >*)’
此功能按预期工作:
template <typename... T>
void Bar(const std::tuple<T...> t, VType<T...>* v) {
v.push_back(t);
}
【问题讨论】:
标签: c++ c++11 templates variadic-templates template-argument-deduction