c++ - 尝试使用可变参数编译代码时出错

标签 c++ templates compiler-errors variadic

当我尝试编译以下代码时,收到 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");
}

error C2672: 'Statement::Bind_All': no matching overloaded function found

error C2783: 'void Statement::Bind_All(Args...)': could not deduce template argument for 'T'

谢谢!

最佳答案

由于 T 不是参数的一部分,编译器无法推断出 T。您需要明确说明。

Statement stmt;
stmt.Bind_All<int>(1, 2.5, "3");

但是,请注意,这将不起作用,因为行中的 '"3"cannot be converted to anint`:

std::vector<T> list = { args... };

调用 stmt.Bind_All() 时,您需要使用合适的参数列表。

关于c++ - 尝试使用可变参数编译代码时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36850092/

相关文章:

iPhone 需要在重新编译之前清理目标

c++ - 在 C++ 中编译多个 .hpp 和 .cpp 文件时出错

java - 具有类 Java API 的 C++ 库

C++ vector 中的元素相乘

c++ - 单个函数的两个可变参数模板?

python - 查看 Django 模板中的所有对象

javascript - 如何绕过AngularJS中的模板缓存?

Swift 扩展,调用中参数 #1 缺少参数

C++ 运算符重载问题 : expected initializer before '<<' token

java - 如何为字符串分配内存?