WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

C++ · Advanced · question 55 of 100

What are variadic templates in C++? Provide an example of their usage.?

📕 Buy this interview preparation book: 100 C++ questions & answers — PDF + EPUB for $5

Variadic templates is a C++11 feature that allows creating functions and classes that accept a variable number of arguments. This feature enables the creation of functions that can take an arbitrary number of arguments of different types, which makes them more flexible and easier to use.

Variadic templates are based on the concept of parameter packs, which are a sequence of zero or more template parameters. Parameter packs can be either type packs or value packs. A type pack is a sequence of zero or more type names enclosed in angle brackets <>. A value pack is a sequence of zero or more values of a specific type.

To define a variadic template function, the ellipsis notation ... is used to indicate the parameter pack. For example, consider the following code snippet that defines a variadic template function sum that takes a variable number of arguments and returns their sum:

    template<typename... Args>
    auto sum(Args... args) {
        return (args + ...);
    }

Here, the parameter pack Args is declared using the ellipsis notation. The function body uses the fold expression (args + ...), which expands the parameter pack args and applies the binary operator + to all its elements. The return type of the function is deduced automatically by the compiler using the trailing return type syntax auto.

We can use the sum function to add any number of integers as follows:

    int s1 = sum(1, 2, 3, 4, 5);      // s1 = 15
    int s2 = sum(1, 2, 3, 4, 5, 6);   // s2 = 21

Variadic templates can also be used to create recursive templates that operate on nested structures. For example, consider the following code that defines a print function that can print the contents of any STL container:

    template<typename T>
    void print(const T& t) {
        std::cout << t << std::endl;
    }
    
    template<typename T, typename... Args>
    void print(const T& t, const Args&... args) {
        std::cout << t << ", ";
        print(args...);
    }

Here, the print function is defined using two overloads. The first overload is a base case that prints a single argument of any type. The second overload is a recursive case that prints the first argument, followed by a comma and a space, and then recursively calls itself with the remaining arguments.

We can use the print function to print the contents of a vector, a set, and a map as follows:

    std::vector<int> v = {1, 2, 3, 4, 5};
    std::set<std::string> s = {"apple", "banana", "cherry"};
    std::map<int, std::string> m = {{1, "one"}, {2, "two"}, {3, "three"}};
    
    print(v);           // prints: [1, 2, 3, 4, 5]
    print(s);           // prints: apple, banana, cherry
    print(m);           // prints: [1, one], [2, two], [3, three]
Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic C++ interview — then scores it.
📞 Practice C++ — free 15 min
📕 Buy this interview preparation book: 100 C++ questions & answers — PDF + EPUB for $5

All 100 C++ questions · All topics