Namespaces
Variants
Actions

Talk:cpp/utility/apply

From cppreference.com

http://dbj.org (talk) 01:21, 5 October 2017 (PDT)

Regarding the example code in http://en.cppreference.com/w/cpp/utility/apply

By using generic lambda it is possible to have generic addition in this snippet.

 /* dbj.org */ #include <iostream> #include <tuple> /* following of course does not work. this is because .... ? */ template<typename T> T adder(T first, T second) { return first + second; } */ /* following generic lambda is the solution */ auto adder = [](auto first, auto second){return first + second; }; int main() { std::cout << std::apply(adder, std::make_pair(1,2)) << '\n'; std::cout << std::apply(adder, std::make_tuple(2.0f,3.0f)) << '\n'; } 

Thanks Cubbi for accepting.

http://dbj.org (talk) 01:21, 5 October 2017 (PDT)

the example isn't a problem to solve.. but it's a fair point, edited to show how a lambda works. --Cubbi (talk) 06:32, 5 October 2017 (PDT)

[edit] noexcept specification of std::apply

when noexcept(apply(f, args...)) == true ? or it's always false?

the function template std::apply does not have a noexcept specification. --Cubbi (talk) 06:32, 5 October 2017 (PDT)

[edit] Reason for problem with template type deduction

What is the reason behind the problem with deduction for template functions and not for lambda?

 // Will compile just fine std::cout << std::apply(add_generic<float>, std::pair(2.0f, 3.0f)) << '\n'; 
 // Error: can't deduce the function type std::cout << std::apply(add_generic, std::pair(2.0f, 3.0f)) << '\n'; 


217.67.201.162 03:19, 24 November 2022 (PST)jarzyn

close