60

I would like to do

template<typename... ArgTypes> void print(ArgTypes... Args)
{
   print(Args)...;
}

And have it be equivalent to this quite bulky recursive chain:

template<typename T, typename... ArgTypes> void print(const T& t, ArgTypes... Args)
{
  print(t);
  print(Args...);
}

followed by explicit single-parameter specializations for every type I'd like to print.

The "problem" with the recursive implementation is that a lot of redundant code is generated, because each recursive step results in a new function of N-1 arguments, whereas the code I'd like to have would only generate code for a single N-arg print function, and have at most N specialized print functions.

0

3 Answers 3

92

C++17 fold expression

(f(args), ...);

If you call something that might return an object with overloaded comma operator use:

((void)f(args), ...);

Pre-C++17 solution

The typical approach here is to use a dumb list-initializer and do the expansion inside it:

{ print(Args)... }

Order of evaluation is guaranteed left-to-right in curly initialisers.

But print returns void so we need to work around that. Let's make it an int then.

{ (print(Args), 0)... }

This won't work as a statement directly, though. We need to give it a type.

using expand_type = int[];
expand_type{ (print(Args), 0)... };

This works as long as there is always one element in the Args pack. Zero-sized arrays are not valid, but we can work around that by making it always have at least one element.

expand_type{ 0, (print(Args), 0)... };

We can make this pattern reusable with a macro.

namespace so {
    using expand_type = int[];
}

#define SO_EXPAND_SIDE_EFFECTS(PATTERN) ::so::expand_type{ 0, ((PATTERN), 0)... }

// usage
SO_EXPAND_SIDE_EFFECTS(print(Args));

However, making this reusable requires a bit more attention to some details. We don't want overloaded comma operators to be used here. Comma cannot be overloaded with one of the arguments void, so let's take advantage of that.

#define SO_EXPAND_SIDE_EFFECTS(PATTERN) \
        ::so::expand_type{ 0, ((PATTERN), void(), 0)... }

If you are paranoid afraid of the compiler allocating large arrays of zeros for naught, you can use some other type that can be list-initialised like that but stores nothing.

namespace so {
    struct expand_type {
        template <typename... T>
        expand_type(T&&...) {}
    };
}
6
  • 1
    "but stores nothing" -- The compiler will likely allocate the same amount of space for that, since arguments need space too (and maybe more, for callstack info etc). And if you speculate on that getting optimized out, I think the array storage is just as-likely to be optimized out.
    – Xeo
    Jun 27, 2013 at 10:02
  • 13
    It's funny how a more advanced C++ will inevitably lead to more advanced hacks to work around things not possible in a clear and concise way. Thanks for writing this up!
    – rubenvb
    Jun 27, 2013 at 12:24
  • 4
    absolutely brilliant answer :)
    – Gabriel
    Oct 2, 2015 at 17:17
  • 1
    Would it also be possible to just call an empty function namespace so { template <typename... T> expand_type(T&&...) {} } ? (or is there some potential dangerous side effect because of the compilere optimization?)
    – Gabriel
    Oct 2, 2015 at 17:43
  • 7
    A function call doesn't guarantee evaluation order. {}-initialization does. Oct 2, 2015 at 17:44
24

C++17 fold expression:

(f(args), ...);

Keep simple things simple ;-)

If you call something that might return an object with overloaded comma operator use:

((void)f(args), ...);
4
  • Can this be part of the accepted answer? REALLY the best answer for C++17
    – Short
    Sep 21, 2018 at 19:12
  • 4
    I've added the C++17 solution to the accepted answer, thanks for the suggestion ;-) Nov 8, 2018 at 16:54
  • Why does the comma folding need outer parentheses? Mar 21, 2020 at 18:36
  • 1
    @Silicomancer Its a C++17 fold expression and therefor its required by fold expression syntax. Mar 21, 2020 at 18:56
7

You can use even more simple and readable approach

template<typename... ArgTypes> void print(ArgTypes... Args)
{
   for (const auto& arg : {Args...})
   {
      print(arg);
   }
}

I have played with both variants on compile explorer and both gcc and clang with O3 or O2 produce exactly the same code but my variant is obviously cleaner.

6
  • 1
    Does this not take a copy regardless of what was passed in? Couldn't this be quite deadly when printing a huge object?
    – rubenvb
    Jun 17, 2016 at 9:38
  • 1
    Als, your link goes to an unrelated snippet.
    – rubenvb
    Jun 17, 2016 at 10:00
  • 2
    While this does indeed look Pretty... pretty, you should probably write something like [...]using value_type = std::common_type_t<Args...>; for (auto const &arg : {static_cast<value_type>(Args)...})[...] in order to allow it to work with heterogeneous parameter packs. Also @rubenvb I believe your concerns are regarding the std::initializer_list<> being copy-initialized?
    – mbw
    Aug 1, 2016 at 19:17
  • 1
    That is of course std::common_type_t<ArgTypes...>.
    – mbw
    Aug 1, 2016 at 19:27
  • Does this work if there are no arguments? Sep 24, 2021 at 10:58

Your Answer

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

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