Skip to content

Last updated:2026-10-12

C++ Variadic Templates

Variadic Templates from C++那些事

Recently, while reading the Arrow source code, I came across an interesting design: multiple nodes, such as proj and agg, can be added to a single plan.

Today let's talk about the design and implementation behind this.

1.Adding nodes

A plan usually needs a whole bunch of nodes added to it, and this is done by calling EmplaceNode.

return plan->EmplaceNode<ScalarAggregateNode>(
    plan, std::move(inputs), schema(std::move(fields)), std::move(target_field_ids),
    std::move(aggregates), std::move(kernels), std::move(states),
    std::move(owned_options));

Similarly, adding a proj node looks like this:

return plan->EmplaceNode<ProjectNode>(plan, std::move(inputs),
                schema(std::move(fields)), std::move(exprs),
                project_options.async_mode);

So what is the magic that makes the code this smooth?

Here is a simplified version of Plan:

class ARROW_EXPORT ExecPlan
{
    public:
    template <typename Node, typename... Args>
      Node* EmplaceNode(Args&&... args) {
        std::unique_ptr<Node> node{new Node{std::forward<Args>(args)...}};
        auto out = node.get();
        AddNode(std::move(node));
        return out;
      }
 }

This syntax looks very complicated, and indeed it is - it is the variadic template introduced after C++11. To explain what this code means: it forwards the parameter pack to the corresponding Node constructor. This example may look a bit complicated, so next let's move on to the demo.

2.Demo

Next we create a graph containing two different nodes, node A and node B. For simplicity, a function template EmplaceNode is used here.

NodeA* nodeA = EmplaceNode<NodeA>("Node A", 10, 3.14);
NodeB* nodeB = EmplaceNode<NodeB>(1, "Node B");

The complete code is as follows:

#include <iostream>
#include <vector>
#include <memory>

class NodeA {
public:
    NodeA(const std::string& name, int value, double weight)
        : name_(name), value_(value), weight_(weight) {}

    const std::string& GetName() const {
        return name_;
    }

private:
    std::string name_;
    int value_;
    double weight_;
};

class NodeB {
public:
    NodeB(int id, const std::string& desc)
        : id_(id), desc_(desc) {}

    int GetID() const {
        return id_;
    }

private:
    int id_;
    std::string desc_;
};
template<typename Node, typename... Args>
Node* EmplaceNode(Args&&... args) {
    std::unique_ptr<Node> node{new Node{std::forward<Args>(args)...}};
    auto out = node.get();
    // AddNode(std::move(node));
    return out;
}

int main() {
    NodeA* nodeA = EmplaceNode<NodeA>("Node A", 10, 3.14);
    NodeB* nodeB = EmplaceNode<NodeB>(1, "Node B");
    std::cout << "Node A name: " << nodeA->GetName() << std::endl;
    std::cout << "Node B ID: " << nodeB->GetID() << std::endl;
    return 0;
}

3.Extension: implementing a print function that can print data of any type

A simple implementation:

It can be implemented with variadic template parameters plus recursion.

Each call peels off one element plus the rest of the pack, until only a single element is left. Hence there is one function that handles a single element and one function that handles one element plus the rest, corresponding to the first print_impl and the second print_impl respectively.

#include <iostream>
#include <utility>

template <typename T>
void print_impl(T&& t) {
    std::cout << t;
}

template <typename T, typename... Args>
void print_impl(T&& t, Args&&... args) {
    std::cout << t;
    print_impl(std::forward<Args>(args)...);
}

template <typename... Args>
void print(Args&&... args) {
    print_impl(std::forward<Args>(args)...);
}

int main() {
    int x = 1;
    const char* str = "Hello, world!";
    print("The value of x is ", x, ", and the string is ", str, ".\n");
    return 0;
}

Comments