Questions tagged [stdapply]

The C++17 std::apply() function takes a function and a tuple and calls the function with the contents of the the tuple as its arguments.

std::apply invokes a callable object with arguments from a collection, whose type supports std::get and std::tuple_size methods. Examples of such collections are std::tuple, std::array and std::pair.

Some pre-C++17 compilers support experimental version of std::apply.

20 questions
10
votes
2 answers

Using std::apply with variadic packs

I am trying to create a generic class that takes a pack of types, stores them in a tuple, and can apply a function over them. What I tried so far is the following: #include struct Base{ virtual void base_function() =…
sqrtroot
  • 167
  • 2
  • 9
10
votes
2 answers

How does std::apply forward parameters without explicit std::forward?

Consider possible implementation of std::apply: namespace detail { template constexpr decltype(auto) apply_impl(F &&f, Tuple &&t, std::index_sequence) { return std::invoke(std::forward(f),…
W.F.
  • 13,888
  • 2
  • 34
  • 81
9
votes
3 answers

std::apply and constant expression?

I tried code below in Wandbox: #include #include #include #include #include #include int main() { constexpr std::array str{"123456789"}; constexpr auto foo =…
Cu2S
  • 667
  • 1
  • 5
  • 21
6
votes
1 answer

Applying a variadic function with std::apply

Is it possible to apply a variadic function to a tuple with std::apply? For example, the following code works fine with GCC 6.2.1: void print_t(std::string i, std::string j) { std::cout << i << " " << j << std::endl; } int main() { …
Sergey
  • 7,985
  • 4
  • 48
  • 80
5
votes
2 answers

Unable to use std::apply on user-defined types

While implementing a compressed_tuple class for some project I'm working on, I ran into the following issue: I can't seem to pass instances of this type to std::apply, even though this should be possible according to:…
4
votes
0 answers

How to implement something like std::apply in C++11?

Is there a way to implement std::apply in c++11? I have a std::tuple myTuple and a std::function myFunction How can I invoke myFunction and pass it the contents of myTuple as arguments in C++11?
Avi Shukron
  • 6,088
  • 8
  • 50
  • 84
3
votes
1 answer

unexplained warning when using std::apply on empty tuple with msvc

I have a warning about unreferenced variable when using std::apply on an empty tuple. This snippet, inspired from std::apply cppreference shows the issue: #include #include #include template
Oersted
  • 769
  • 16
3
votes
1 answer

Should std::apply only apply to std::tuple?

The function signature of std::apply does not constrain the template parameter Tuple to be a specialization of std::tuple, so it can still accept a tuple-like objects that defines std::tuple_size_v (godbolt): #include #include…
康桓瑋
  • 33,481
  • 5
  • 40
  • 90
3
votes
1 answer

How can you have multiple lines or statements inside a C++ pack expansion?

Suppose that I have some simple code like the one below, which just prints all of the values in a tuple and keeps track of the current iteration. #include #include #include using std::cout; int main() { …
Doot
  • 555
  • 5
  • 15
3
votes
2 answers

std::visit a std::variant with overloaded free-function instead of function-object

In C++17 is there a simple way to std::visit a variant with an overloaded free-function or must I use an object with an overloaded call operator? In other words, is it possible to add something simple to make the following //ERROR! line compile to…
Timtro
  • 418
  • 5
  • 15
2
votes
1 answer

std::apply-ing sscanf into a tuple, tuple not fully updating

I have a structured string of data, and I want to parse it into a tuple. For each different kind of input string, the types and arrangement of the data can be different, so I want to use templates and scanf formats to avoid having to manage all…
Remy Porter
  • 353
  • 1
  • 3
  • 12
2
votes
3 answers

Using std::apply on class method

I'm trying to get the following to compile (g++-11.2, C++20), but I get: error: no matching function for call to '__invoke(std::_Mem_fn, std::__tuple_element_t<0, std::tuple >, std::__tuple_element_t<1,…
Agrim Pathak
  • 3,047
  • 4
  • 27
  • 43
1
vote
2 answers

How can I assign element-wise to a tuple using fold expressions?

I have a type effectively wrapping a variadic std::tuple like this: #include #include template struct Foo { std::tuple t; Foo(Args&&... a) : t{ std::forward(a)... } { } …
Jodocus
  • 7,493
  • 1
  • 29
  • 45
1
vote
1 answer

std::function vs callable as template parameter

In the example below, why line 20 causes the error described from line 27 to 30? Calling exec1 in line 33 works fine. #include #include #include #include #include template
1
vote
1 answer

Generically wrap member function of object to modify return type

Version restriction: C++17. I'm trying to create a type capable of accepting a callable object of any type and wrapping one of its member functions (in this case, operator()) to take the same arguments, but modify (cast) the return type. An example…
user4520
  • 3,401
  • 1
  • 27
  • 50
1
2