'How to pass template variadic arguments by reference in C++?

I want to get repeated inputs, therefore I made the function to get inputs with variety of types.

template <typename InputType>
void get_inputs(const std::string& placeholder, InputType& input){
    std::cout << placeholder << ": ";
    std::cin >> input;
}

template <typename InputType, typename... Args>
void get_inputs(const std::string& placeholder, InputType& input, Args... args){
    get_inputs(placeholder, input);
    get_inputs(args...);
}

int main(int, char**) {
    std::string name;
    int age;
    double height;

    get_inputs(
        "name", name,
        "age", age,
        "height", height
    );

    std::cout << "INFO: " << name << ", " << age << ", " << height << std::endl;

    return 0;
}

And if I enter like (the content enclosed in square bracket is user input)

Input

name: [example-name]
age: 20
height: 173.5

then the result must be

Output

INFO: example-name, 20, 173.5

However, the result is

Output

INFO: example-name, 1, 2.12785e-314

I noticed that only the first argument was passed by reference, and the remains were passed by value. I think the problem caused by not well-defined get_inputs functions. How can I solve it?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source