24

I have seen a lot of links introducing the variadic templates. But I have never seen any compilable example that demonstrates this approach.

Could someone provide me with some links in which such compilable examples can be found?

4
  • 1
    What do you mean by "compilable example"? I think all those links introduced examples which can be compiled.
    – kennytm
    Sep 3, 2010 at 9:17
  • 1
    no, that is not the case. This examples describe only the deceleration of such veriadic temlates may be the definition too, but never the call, in case of variadic function, or instantiate some variadic classes in the main function. I need actually just simple example which I can copy and compile it to better understand the variadic mechanism..
    – sami
    Sep 3, 2010 at 9:23
  • 1
    Are you sure you're using a compiler that supports it and invoke it with the correct options?
    – sellibitze
    Sep 3, 2010 at 12:36
  • Actually, I just meant that I need some examples to compile it. I do know that the compile flag -std=c++0x should be set. However all example I found are printf like. I need a small but meaningful example..
    – sami
    Sep 3, 2010 at 14:17

8 Answers 8

28

One of the simplest possible examples is the following implementation of max which isn't even templated on types.

int maximum(int n)
{
    return n;
}

template<typename... Args>
int maximum(int n, Args... args)
{
    return max(n, maximum(args...));
}

Only slightly more complex is the canonical printf implementation:

void printf(const char *s)
{
  while (*s)
  {
    if (*s == '%' && *(++s) != '%')
      throw "invalid format string: missing arguments";
    std::cout << *s++;
  }
}

template<typename T, typename... Args>
void printf(const char* s, T value, Args... args)
{
  while (*s)
  {
    if (*s == '%' && *(++s) != '%')
    {
      std::cout << value;
      printf(s, args...); // call even when *s == 0 to detect extra arguments
      return;
    }
    std::cout << *s++;
  }
  throw "extra arguments provided to printf";
}
5
  • 1
    Are the ... after Args and args just syntactic salt, or are there also ways in which variadic parameters can be used without the ...?
    – Stewart
    Oct 18, 2011 at 16:20
  • 2
    The placement of ... matters. Consider two variadic template functions sum and product. Now call them from within another variadic template with parameters 2,3,4 the value of product(sum(args...)) will be 9 and the value of product(sum(args)...) will be 24.
    – Motti
    Oct 26, 2011 at 20:51
  • @Stewart I expanded on this theme slightly in a blog post
    – Motti
    Nov 7, 2011 at 15:37
  • does the line printf(s, args...); mean a recursive runtime call to another function compiled with 1 less argument or does the compiler recursively expand this line to body of printf with 1 less argument?
    – Youda008
    Dec 14, 2014 at 18:25
  • @youda008 it's not technically recursion, the compiler instanciates a new overload of printf with one less parameter and adds a call to this function. This function call may, or may not, be inlined but that's irrelevant.
    – Motti
    Dec 14, 2014 at 19:08
11

Variadic templates are a C++0x feature that primarily targets authors of generic libraries. I would not expect to see them in "user code". For example, in the C++0x standard library they are used in a lot of places: std::function, std::async, std::reference_wrapper, std::tuple, std::packaged_task, ...

To give you an example I'll show you how a reference_wrapper might be implemented with respect to variadic templates:

template<class T>
class reference_wrapper
{
    T *ptr;
public:
    explicit reference_wrapper(T& thing) : ptr(&thing) {}
    explicit reference_wrapper(T&&     ) = delete;

    operator T&() const {return *ptr;}

    template<class... Args>
    decltype( declval<T&>()(declval<Args>()...) )
    operator()(Args&&... args) const
    {
        return (*ptr)(forward<Args>(args)...);
    }
};

This is not perfectly conforming to the standard draft but it is supposed to be compilable with little modification. It demonstrates multiple C++0x features:

  • deleted functions (disabling the constructor for rvalues)
  • rvalue references (detecting rvalue arguments to the constructor, perfect forwarding)
  • type deduction via decltype
  • standard library function template declval to create objects for the purpose of building an expression for decltype (GCC does not yet offer this function template. You have to write it yourself)
  • variadic templates (accepting an arbitrary number of parameters)

The purpose of the variadic member template is to forward arguments to the object referred to by ptr. This should work in case T is a function pointer type or a class type with overloaded function call operator.

cheers! s

6
1
  • 2
    yes, I looked at it. I tried to create an example to to compile it without success. I need an compilable example which, as I said, demonstrates this approach by running it
    – sami
    Sep 3, 2010 at 9:28
6

A very simple example of variadic template:

Suppose we want to have a function which takes variable number of arguments and prints them all. For ex:

print("Hello", 1, 3.14, 5L);

For that functionality to work, we would basically require two functions:

First one, a function which takes variable number of arguments:

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

Some explanation:

1.) Parameter Packs denoted by ellipsis(...), that appear in parameter list.

typename...Args 
        |  | << Optional whitespace. Can have multiple whitespaces in between them
    Args...args

That means, these all are same.

typename ...args
typename...args
typename   ...   args

So, you don't have to worry about the correct position of the whitespace in there. Though, IMO at most one whitespace should be used as a best practice.

2.) Pack Expansion: A pattern followed by an ellipsis.

print(args...); //expand when you wish to use them

3.) Parameter pack accepts zero or more template args. So, print(T t, Args... args) accepts one or more args.


Once you understand that, we can visualize the call flow as below:

print("Hello", 1, 3.14, 5L);

translates into:

print(string, int, float, long);

which calls

print(int, float, long);

which calls

print(float, long);  // say Level 2

which calls

print(long);         // say Level 1

which calls

print();             // say Level 0

If you have followed the Point#3 carefully, you must have realized that print(T t, Args... args) can't handle call at Level 0.
So we need another function here with same name to catch up at any level >=0.


Second one, a function to grab the call at the top of call stack:

Catch at level 0:

void print(){}

or, Catch at level 1:

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

or, Catch at level 2:

template<typename T, typename U>
void print(T t, U u){ std::cout << t << ", " << u;}

so on...

Any of these would work. Hope this helps you next time you go about writing such function or class.

1
  • 1
    Thank you for your thorough and easy-to-follow answer. I can't believe I found something like this all the way at the bottom of the answers. Aug 26, 2018 at 6:02
4

This is an example of variadic templates that I put up on my blog: http://thenewcpp.wordpress.com/2011/11/23/variadic-templates-part-1-2/

It compiles. It demonstrates finding the largest type from a group of types.

#include <type_traits>

template <typename... Args>
struct find_biggest;

//the biggest of one thing is that one thing
template <typename First>
struct find_biggest<First>
{
  typedef First type;
};

//the biggest of everything in Args and First
template <typename First, typename... Args>
struct find_biggest<First, Args...>
{
  typedef typename find_biggest<Args...>::type next;
  typedef typename std::conditional
  <
    sizeof(First) >= sizeof(next),
    First,
    next
  >::type type;
};
2
  • 3
    Mmmmm i've always thought that C++ was made for a more human readable syntax than C...
    – Virus721
    Nov 27, 2013 at 14:10
  • Maybe in some cases. But you can't even do this in C, so it's not really comparable.
    – Jarryd
    Nov 27, 2013 at 23:56
2

Variadic templates are part of the C++0x standard which is not yet officially released. They are supported by gcc since version 4.3, but you need to enable support for C++0x by adding the compiler switch -std=c++0x.

3
  • Right. And as far as I know, the Microsoft Compiler doesn't yet support variadic templates. Just saying.
    – sellibitze
    Sep 3, 2010 at 12:35
  • They announced some features that will be in MSVC11 (in 2012), and Variadic Templates was not on that list. Time for me to learn to install MinGW. Nov 23, 2011 at 22:11
  • 1
    @Virus721 he was complaining that there is no compilable example. There are tons of examples - the only reason they don't compile are: missing compiler support or missing compiler switch Nov 28, 2013 at 7:19
0

Before C++11, you can create template only with the fixed count of parameters.

Firts template for the function with one parameter.

Second template for the function with two parameters. ... i.e.

Since C++11 you can write only one template, compiler will generate required function itself.

Good example http://eli.thegreenplace.net/2014/variadic-templates-in-c/

0

another syntax: expanding, e.g.

template<typename VAL, typename... KEYS>
class MyMaps
{
  typedef std::tuple< std::map<KEYS,VAL>... > Maps;
}

hence:

MyMaps<int,int,string>:Maps

is now actually:

std::tuple<std::map<int,int>,std::map<string,int> >

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.