C++ parameter pack as variadic template

140 views Asked by At

I tried to build a function with a variable number of arguments which are of different types (string, double, int, bool). For this purpose I uses a variadic template. The function arguments are passed to a std::tuple, whose values will be extracted later. But my application runs into compile errors.

Error: Error C2440 "Initialisation": "char *" cannot be converted in "int" (translated to EN). This for the "int" argument, that obviously will be recognized as "char *" by the compiler. Commenting/Deleting the errorneous lines, the type recongnized during runtime ist correctly "int".

IDE: Visual Studio 2017 Community Edition, V 15.5.6

Example: An easy code example, that has all essential features of the proper project is attached below. NB: The compilation of this code example works fine, no errors and variable types/Values and output as expected:

#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

There are 1 answers

0
Ivan Sanz Carasa On

Looks like you tried to do something like:

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

This should work:

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