C ++参数包作为可变参数模板

时间:2018-02-12 12:53:18

标签: c++ tuples c++14 std

我尝试使用可变数量的不同类型的参数(string,double,int,bool)构建一个函数。为此,我使用了一个可变参数模板。函数参数传递给std :: tuple,稍后将提取其值。但我的应用程序会遇到编译错误。

错误: 错误C2440"初始化":" char *"无法转换为" int" (翻译为EN)。 这适用于" int"论证,显然会被认为是" char *"由编译器。注释/删除错误的行,在运行时正确识别的类型是正确的" int"。

IDE: Visual Studio 2017社区版,V 15.5.6

示例: 下面附有一个简单的代码示例,其中包含正确项目的所有基本功能。注意:此代码示例的编译工作正常,没有错误和变量类型/值和输出按预期:

#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;
}

1 个答案:

答案 0 :(得分:1)

看起来你试图做类似的事情:

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

这应该有效:

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