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
1
vote
4 answers

Fold Expression in C++17

I'm reading "C++17 - The Complete Guide" book and I came across the example on page 107 and 108 regarding fold expression in C++17: template void print(First first, const Args&... args) { std::cout << first; …
Ali
  • 288
  • 3
  • 10
1
vote
1 answer

I'm trying to create an exception class that uses a parameter pack constructor in c++

I am creating custom exception class called app_exception which is derived from runtime_exception. I want to put multiple arguments in the constructor, but I can't figure out why the code will not compile. I normally use va_start with ..., but I'm…
1
vote
2 answers

Improving fold function

I have implemented a simple fold function in C++ that accepts a lambda, and can fold multiple vectors at the same time at compile time. I am wondering if it could be simplified in some manner (I have provided both a recursive version and an…
1
vote
1 answer

Evaluating a parameter pack

Is this the only way to evaluate a parameter pack without using folding (since it requires the use of operators)? #include template void eval(Function&& f) { // (f(Is)...); auto op = [&f](int…
lightxbulb
  • 1,251
  • 12
  • 29
1
vote
2 answers

Join outputs from fold/variadic expression with && operator

Function all_checked is meant to return true if all calls to parse(...) returned true, and false otherwise. How can I link all the outputs together so that I effectively get success = parse(args[0]) && parse(args[1]) && parse(args[2]) && ...; Right…
Phil-ZXX
  • 2,359
  • 2
  • 28
  • 40
1
vote
1 answer

Variadic functions and fold expression: Fatal Error while trying to compile in Visual Studio 2017

I was reading this Q/A and some of the answers provided a possible solution for using Fold Expressions in C++17. I thought I'd try this technique within my own code. This is what I have tried: Some Header #pragma once #include #include…
1
vote
1 answer

About Logical operator using variable parameter in templates

I don't know what's happening in this code. SimpleFunction(1,2,3) is equal to 1&&(2&&3) //1 1||(2||3) //1 SimpleFunction(1) is equal to 1&&Something //1 1||Something //1 SimpleFunction() is equal to voidvalue (&&) //1 voidvalue (||) //0 What…
Scha
  • 13
  • 2
1
vote
1 answer

Compiler error with a fold expression in enable_if_t

I have the following code, where I am using a fold expression to evaluate whether all pack parameters are convertible to the first function argument. For some reason it fails to compile on msvc when I make what seems like a very trivial…
lightxbulb
  • 1,251
  • 12
  • 29
1
vote
1 answer

Can I use a Fold Expression in this example

I want to know if it is possible to use fold expression (and how to write it) in the example below. #include #include #include #include #include template std::string padFormat() { …
Blood-HaZaRd
  • 2,049
  • 2
  • 20
  • 43
1
vote
2 answers

Are fold expressions fully supported in VS2017?

I found an interesting article and tried its code with MSVS 2017: #include #include template void for_each(const std::tuple& t, Func&& f, std::index_sequence)…
1
vote
2 answers

c++17 fold expression syntatx

I have problem of make code working of fold expression in line 18 and line 23 I want make it such as have this result "1 2 3 4" "9 0 -1 -200" "abc" " world" "Empty List" As if the list is empty you will print "Empty List", if not, if the type is…
1
vote
1 answer

Can this code that expands integer parameter pack be written with just 1 function?

I have code that uses fold expressions to compare function argument against integer parmeters of class template. Code works AFAIK, but I wonder if it is possible to do what I want without _impl helper function. Full code(my question is if contains…
NoSenseEtAl
  • 28,205
  • 28
  • 128
  • 277
0
votes
1 answer

C++ fold expressions, how to apply it with a function instead of an operator?

I have a set of messages, some of which have variable sizes but still with fixed maximum sizes. I want to get the maximum size amongst that set of messages. template< typename... Messages > struct BinaryMessageList { static constexpr size_t…
hlide
  • 237
  • 2
  • 10
0
votes
1 answer

How to expand a NTTP parameter pack with a fold expression

I have a function (bar) that takes a pack of NTTP, hax can I expand the pack using fold expression so that each element of the pack is the template parameter of another function (foo). #include #include #include…
monamimani
  • 273
  • 2
  • 11
0
votes
1 answer

C++ varadic template function - only one instance of known parameter pack

I want to use a fold expression but the function will only ever be used with one known parameter pack. i.e. template Foo fn_impl(Arg arg) { // here, a fold expression involving Types... that returns a Foo } Foo fn(Arg arg) { …
Nebular Noise
  • 388
  • 3
  • 15