C++ Logo

std-discussion

Advanced search

Re: generic c++ for loop

From: Yongwei Wu <wuyongwei_at_[hidden]>
Date: Tue, 26 Nov 2024 10:48:10 +0800
On Tue, 26 Nov 2024 at 04:13, Smith, Jim via Std-Discussion <
std-discussion_at_[hidden]> wrote:

> Hi All,
>
> Is there already a proposal for making the C++ for loop generic, for
> example able to print a function parameter pack?
>

As Andrey said, fold expressions can usually do. Specifically, with the
comma operator.

This is an example I usually use in training:

template <typename T, typename... Args>

void printWithSeparator(const T& sep, const Args&... args)

{

    ((cout << args << sep), ...);

}

Replace with the `(cout << args << sep)` part with what you want to do with
the arguments.

--
Yongwei Wu
URL: http://wyw.dcweb.cn/

Received on 2024-11-26 02:48:25