#define LAMBDA(x) [&](auto&&... inputs) { return x(inputs...); }
std::vector<int> v;
auto func = v.push_back; //Proposed syntax
std::vector<int> v;
//This will compile if LAMBDA is defined as above
auto func = LAMBDA(v.push_back);
template<class Func,class... Args>
void RepeatNTimes(size_t n, Func&& function,Args&&... arguments) {
for(size_t i=0;i<n;++i) {
function(arguments...);
}
}
//...
//Later in the code:
std::vector<int> v {};
RepeatNTimes(5, v.push_back, 0); //Proposed syntax
//v now equals {0, 0, 0, 0, 0}
RepeatNTimes(5, LAMBDA(v.push_back), 0);//This will compile if LAMBDA is defined as above
--
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-pr...@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/78d38a05-9633-43af-a04f-7a3895b72d53%40isocpp.org.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposal...@isocpp.org.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-pr...@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/ced4dd3b-4eaa-4e41-a91f-b78e40564933%40isocpp.org.
//In Demo.cc:
#include <iostream>#include <vector>#include <functional>#include <utility>
struct movable { int id; bool moved = false; movable() { static int num_created = 0; id = num_created++; std::cout << "[movable() called, id=" << id << "]" << std::endl; } movable(const movable& m) : id(m.id) { std::cout << "[movable(const&) called, id=" << id << "]" << std::endl; } movable(movable&& m) : id(m.id) { m.moved = true; std::cout << "[movable(&&) called, id=" << id << "]" << std::endl; } ~movable() { if(!moved) { std::cout << "[~movable() called, id=" << id << "]" << std::endl; } }};
void move_and_discard(movable& m) { std::cout << "[recieved reference; didn't move]" << std::endl;}void move_and_discard(movable&& m) { movable dest = std::move(m); std::cout << "[moved object]" << std::endl;}int main() { { std::cout << "Direct call: " << std::endl; movable A {}; move_and_discard(std::move(A)); } { auto move_and_discard_lambda = [](auto&&... inputs) { move_and_discard(inputs...); }; std::cout << "With lambda (unforwarded): " << std::endl; movable B {}; move_and_discard_lambda(std::move(B)); } { auto move_and_discard_lambda = [](auto&&... inputs) { move_and_discard(std::forward<decltype(inputs)>(inputs)...); }; std::cout << "With lambda (forwarded): " << std::endl; movable C {}; move_and_discard_lambda(std::move(C)); }}/* Terminal output:
$ c++-7 -std=c++17 -O3 -pedantic -Wall Demo.cc -o Demo && ./Demo
Direct call:
[movable() called, id=0]
[movable(&&) called, id=0]
[moved object]
[~movable() called, id=0]
With lambda (unforwarded):
[movable() called, id=1]
[recieved reference; didn't move]
[~movable() called, id=1]
With lambda (forwarded):
[movable() called, id=2]
[movable(&&) called, id=2]
[moved object]
[~movable() called, id=2]
*/
--
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-pr...@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/78d38a05-9633-43af-a04f-7a3895b72d53%40isocpp.org.