简体   繁体   中英

How to iterate over variadic function with std:string arguments?

void foo(std::string arg, ...) {

   // do something with every argument

}

Lets say I want to be able to take every string argument and append an exclamation mark before printing it out on a new line.

The best way is to use parameters pack . For example:

#include <iostream>

// Modify single string.
void foo(std::string& arg)
{
    arg.append("!");
}

// Modify multiple strings. Here we use parameters pack by `...T`
template<typename ...T>
void foo(std::string& arg, T&... args)
{
    foo(arg);
    foo(args...);
}

int main()
{
    // Lets make a test

    std::string s1 = "qwe";
    std::string s2 = "asd";

    foo(s1, s2);

    std::cout << s1 << std::endl << s2 << std::endl;

    return 0;
}

This will print out:

qwe!
asd!

C++17

Use aparameter pack with a fold expression :

#include <iostream>
#include <string>

// Modify multiple strings. Here we use parameters pack by `...T`
template<typename ...T>
void foo(T&... args)
{
    (args.append("!"),...);
}

int main()
{
    // Lets make a test

    std::string s1 = "qwe";
    std::string s2 = "asd";

    foo(s1, s2);

    std::cout << s1 << std::endl << s2 << std::endl;

    return 0;
}

Here is an iterative solution. There is a bit of noise in the function call, but no computation of the number of varargs is needed.

#include <iostream>
#include <string>
#include <initializer_list>
#include <functional> // reference_wrapper

void foo(std::initializer_list<std::reference_wrapper<std::string>> args) {
    for (auto arg : args) {
        arg.get().append("!");
    }
}

int main() {
    // Lets make a test

    std::string s1 = "qwe";
    std::string s2 = "asd";

    foo({s1, s2});

    std::cout << s1 << std::endl << s2 << std::endl;

    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM