当我尝试编译以下代码时,收到C2672和C2783错误。 我无法弄清楚如何解决它。
class Statement
{
public:
template<typename T, typename ... Args>
void Bind_All(Args... args)
{
std::vector<T> list = { args... };
}
}
void func()
{
Statement stmt;
stmt.Bind_All(1, 2.5, "3");
}
错误C2672:'Statement :: Bind_All':找不到匹配的重载函数
错误C2783:'void Statement :: Bind_All(Args ...)':无法推断'T'的模板参数
谢谢!
答案 0 :(得分:3)
由于T
不是参数的一部分,编译器无法推断出T
。你需要明确说明。
Statement stmt;
stmt.Bind_All<int>(1, 2.5, "3");
但是,请注意,这不会起作用,因为行中的“3”cannot be converted to an
int`:
std::vector<T> list = { args... };
调用stmt.Bind_All()
时,您需要使用合适的参数列表。
答案 1 :(得分:0)
以下是如何完成和使用此可变参数模板的示例:
>>> data = """
lines with tweets here
"""
>>> for line in data.splitlines():
... print(line.split(" ", 7)[-1])
...
rt we're treating one of you lads to this d'struct denim shirt! simply follow & rt to enter
this album is wonderful, i'm so proud of you, i loved this album, it really is the best. -273
international break is garbage smh. it's boring and your players get injured
get weather updates from the weather channel. 15:27:19
woah what happened to twitter this update is horrible
i've completed the daily quest in paradise island 2!
new post: henderson memorial public library
who's going to next week?
why so blue? @ golden bee
或者,如果您想看到实际使用的是不同的类型:
class Statement {
public:
template <typename T>
static void Bind_All(T value) {}
template<typename T,typename ... Args>
static void Bind_All(T value, Args... args)
{
Bind_All(args...);
}
};
void func()
{
Statement stmt;
stmt.Bind_All(1,2.5,"3");
}
int main()
{
func();
return 0;
}
在我的系统上,这会产生:
#include <iostream>
#include <typeinfo>
class Statement {
public:
template <typename T>
static void Bind_All(T value)
{
std::cout << typeid(value).name() << ":";
std::cout << value << '\n';
}
template<typename T,typename ... Args>
static void Bind_All(T value, Args... args)
{
std::cout << typeid(value).name() << ":";
std::cout << value << '\n';
Bind_All(args...);
}
};
void func()
{
Statement stmt;
stmt.Bind_All(1,2.5,"3");
}
int main()
{
func();
return 0;
}