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
0
votes
0 answers

Compile error: function parameter 'args' with unknown value cannot be used in a constant expression

I have this piece of code: template struct Base { constexpr std::uint8_t getValue() noexcept { return static_cast(this)->getValueImpl(); } }; class Derived : public Base { friend struct Base; …
0
votes
1 answer

template function specialization using variadic arguments

I have a class that takes a variable number of arguments (including no arguments) but when I try to pass a struct as argument for its constructor, I get a compile time error: error: converting to 'const ioPin' from initializer list would use…
Juan_David
  • 126
  • 2
  • 11
0
votes
3 answers

C++ 17 Expand a Fold Expression (as pairs of values)

I am writing some code to experiment with fold expressions. I don't think my approach to solving this problem is the best possible approach. I am trying to write a function which "does something" with pairs of values obtained from expanding a…
FreelanceConsultant
  • 13,167
  • 27
  • 115
  • 225
0
votes
2 answers

C++17 can variadic template class define new type and variadic data member?

#include template struct Clazz { void doSomething(const std::shared_ptr& p0, const std::shared_ptr& p1) {} T0 t0; T1 t1; }; template
xishvai
  • 3
  • 2
0
votes
2 answers

Why does the fold expression not apply to for loop?

Below is from an authoritative C++ proposal: template constexpr void tuple::swap(tuple& other) { for...(constexpr size_t N : view::iota(0uz, sizeof...(TYPES))) { swap(get(*this), get(other)); …
xmllmx
  • 39,765
  • 26
  • 162
  • 323
0
votes
1 answer

C++20: generate type from given variadic types

Lets say, I have a variadic type list which can grow e.g. #define MY_TYPES void, float, int, vector, ..... I am looking for way to generate these type definition at compile time in c++17/20 in generic way, e.g. #define to_tuple(MY_TYPES)…
0
votes
1 answer

Lisp like C++ function calling with std::tuple and fold expressions

The most basic thing I can think about Lisp is how to call functions. To those who doesn't know how to do that in Lisp, is something like that: (fun1 a b (fun2 c d)) ; c and d are parameters to the function fun2, ; and a, b and the result of fun2…
0
votes
1 answer

non default constructible parameters within lambdas in fold expressions

I'm trying to see if it's possible to use a parameter that is not default constructible within a lambda as part of a fold expression. Following code works if T is default constructible. template struct X { void foo() { …
user3882729
  • 1,339
  • 8
  • 11
0
votes
1 answer

Create template pack from set of traits

Is it possible (and if so, how) to generate a template pack from a indexed set of type traits so they can be used to instantiate a variant or a tuple? #include template struct IntToType; template<> struct IntToType<0> { using…
user877329
  • 6,717
  • 8
  • 46
  • 88
0
votes
1 answer

Initializer list, parameter pack expansion, fold expressions and order of evaluation

I have the following code to address a n-dimensional tensor class (offset is a std::vector of std::size_t): template double Tensor::at(int first, Ts... others) { int i = 0; std::size_t index =…
0
votes
1 answer

Variadic template function rvalue parameters silently moved C++ to the function

The following code compiles and prints: move move. I would prefer that it didn't compile since merge takes rvalue references and I don't move t1 and t2 to it. class A { public: A() = default; A(const A& other) { std::cout << "copy "; }; …
0
votes
1 answer

Using SFINAE to ensure the Variadic Parameter Pack Arguments are Derived of a Specific Type

I currently have a class hierachy with the following structure: #include #include class Base { protected: std::list> items_; Base(){} public: addItem(Base* derived) { // adds derived to…
Francis Cugler
  • 7,788
  • 2
  • 28
  • 59
0
votes
1 answer

C++ how to iterate through variadic template types and add them to tuple?

This one is rather complex, so I haven't been able to solve it myself. Here's the relevant code, I'll explain more in depth after. #include #include #include #include #include struct Prop { …
0
votes
1 answer

c++ macro expansion(__VA_ARGS__ item name and value)

Is there any trick to achieve the function of the pseudo-code below? THX. template void fake_fold((T1 t1, T2 t2)...) { (std::make_pair(t1, t2)), ...; // std::pair, T1 type is always…
breaker00
  • 167
  • 7
0
votes
0 answers

Expression Template Operators with Variadic Templates

Using variadic templates we can write a function add, which takes any number of input parameters, and then returns a single Expression (to be lazily evaluated). When called for a specific index i, this expression sums all elements at i for all the…
1 2 3
9
10