C 参数包作为变异模板

C++ parameter pack as variadic template

本文关键字:变异 参数 包作      更新时间:2023-10-16

我尝试构建一个具有不同类型的参数(字符串,double,int,bool(的函数。为此,我使用了一个变异模板。函数参数传递给std :: tuple,其值将在稍后提取。但是我的应用程序会陷入编译错误。

错误:错误C2440"初始化":" char *"不能以" int"转换(转换为en(。这是为" int"论点,显然将被编译器认可为" char *"。评论/删除错误的线,该类型在运行时重新处理的类型正确地" int"。

IDE:Visual Studio 2017 Community Edition,V 15.5.6

示例:一个简单的代码示例,具有适当项目的所有基本功能,请下面。NB:此代码示例的汇编工作正常,没有错误,可变类型/值以及预期的输出:

#include<iostream>
#include<tuple>
using namespace std;
template<typename ... T>
int func1(string s, T... args) {
    //creating the tuple ...
    auto argsT = make_tuple(args...);
    //run through the variables
    double p0{ get<0>(argsT) };
    string p1{ get<1>(argsT) };
    int p2{ get<2>(argsT) };
    bool p3{ get<3>(argsT) };
    char p4{ get<4>(argsT) };
    int p6{ get<6>(argsT) };
    string w{ s };
    std::cout << w.c_str() << std::endl;
    double z = p0 * p6;
    return 1;
}
int main()
{
    func1("Kuckuck",1.0, "Hello", 4, false, 'n', "bye", -5, true, 't');
    return 0;
}

看起来您尝试做类似:

的事情
int p5{ get<5>(argsT) };
// "const char *" cannot be converted to "int"

这应该有效:

const char* p5{ get<5>(argsT) };