Questions tagged [fold-expression]

fold expressions, since C++17, are used to reduce the parameter packs of variadic templates over a binary operator.

144 questions
2
votes
1 answer

How to use fold expression to instantiate each type in a variadic template function?

I am trying to create an "object manager" that will create and store instances of a template class. The class takes one template argument and inherits from a base class to allow instances with different template args to be stored in the same…
yah_nosh
  • 155
  • 1
  • 8
2
votes
1 answer

Does the operand to a fold expression need to be parenthesized?

For the following program: #include auto f(auto ...args) { (std::cout << 1 << ... << args); } int main() { f(0, 0, 0); } gcc prints 1000, but clang gives an error: error: expression not permitted as operand of fold expression …
cigien
  • 57,834
  • 11
  • 73
  • 112
2
votes
3 answers

C++17 fold syntax to test vector composition

Standard C++17 implementation of vector any, all: template bool contains(const C& c, const T& value) { return std::find(c.begin(), c.end(), value) != c.end(); } template bool any(const C& c, T&&... value) { …
Charles Pehlivanian
  • 2,083
  • 17
  • 25
2
votes
2 answers

c++ fold expression with user defined class

I'd like make a log function with multiple parameters, and I also want to call the log function with my class. Here is my code (compiler : Visual studio 2019 or x86-64 gcc9.2) Question 1> I cannot understand log function. Is it possible to use fold…
Eric Kim
  • 55
  • 5
2
votes
1 answer

Fold expressions and parameter pack: difference betwen Args&& and Args inside static_assert

This code is from https://en.cppreference.com/w/cpp/language/fold template void push_back_vec(std::vector& v, Args&&... args){ static_assert((std::is_constructible_v && ...)); …
Nick
  • 9,962
  • 4
  • 42
  • 80
2
votes
2 answers

Fold expression with strings and their size

I have C++14 code like this. I am updating to C++17. Is there a way this to be rewritten as fold expression? namespace cat_impl_{ inline size_t catSize(std::string_view const first) { return first.size(); } template
Nick
  • 9,962
  • 4
  • 42
  • 80
2
votes
4 answers

Fold expressions and cout

I have the following code that works, but I am confused about how it works. template void print(Args&&... args) { (std::cout << ... << std::forward(args)) << '\n'; } int main() { print(1,2.0,"3"); } output: 123 My…
NoSenseEtAl
  • 28,205
  • 28
  • 128
  • 277
2
votes
1 answer

fold expression in assignment

I am trying to use a fold expression to simplify some code. In following code, I am trying to insert elements into an array, but the fold expression does not compile struct test { std::string cmd[20]; test() { int i = 0; auto insert =…
2
votes
1 answer

How to detect availability of a parameter of given type in variadic funtion arguments list and act after all parameters have been processed

I have the following operator<<() overload for my class with C++17 folds: template ostream& operator <<(Args&&... args) { //Currently: return (m_osCout << ... << args); //What I need: IF ANY OF THE parameters in…
2
votes
2 answers

Limit allowed COM Interfaces by using template function with limited variadic parameters (std::is_same)

I have wrapper classes contaning COM pointers (or smart pointers) to different interfaces. INTEND: Some COM classes can be obtained from various other COM interfaces, and I want to make a template constructor with variadic types, which would allow…
Sergey Kolesnik
  • 3,009
  • 1
  • 8
  • 28
2
votes
2 answers

c++17 fold expression print function that can support 2,0000 element efficient

Hello how can make it print it more efficient with large element like 20,000 element ? i took very long to compile , i try to follow this static const array of values in Variadic Template C++ but i can't make it work with fold expression, any…
2
votes
1 answer

Create recursive function argument list from one value and a size

My intent is to create a function argument list of size n so I can pass it to a helper that uses a fold expression to recursively multiply the values together. I'm a little stuck on how to make the argument list to pass to the helper. Is there a way…
Casey
  • 10,297
  • 11
  • 59
  • 88
2
votes
0 answers

Are there guarantees on compile complexity of parameter pack expansion using fold expressions

I can imagine a straightforward efficient implementation of fold expression expansion when all types of parameter pack are the same (simplified c++11 example): #include #include template struct bool_pack {…
1
vote
1 answer

Uninitialized variable using IIFE and fold expression in C++

I was playing around with IIFE's and parameter packs in MSVC C++20 when I stumbled across a strange error. The following code is a minimal reproducible example. #include #include template struct Foo { …
BubLblckZ
  • 434
  • 4
  • 14
1
vote
1 answer

Accessing indiviual alias inside another alias that is a parameter pack with a fold expression

Given this types: template struct base_unit { using ratio = r; using symbol = s; }; template struct derived_unit { using units = std::tuple; }; If, for example, I have a type…
Alex Vergara
  • 1,766
  • 1
  • 10
  • 29