I have the following code:
#include <cstdio> template<class Fun, class... Args> void foo(Fun f, Args... args) { f(args...); } int main() { int a = 2; int b = 1000; foo([](int &b, int a){ b = a; }, b, a); std::printf("%d\n", b); }
It is currently printing 1000 , meaning the new value of b is lost somewhere. I assume that since foo passes parameters in a parameter package by value. How can i fix this?
1000
b
foo
Using the link:
template<class Fun, class... Args> void foo(Fun f, Args&&... args) { f( std::forward<Args>(args)... ); }
like this:
#include <iostream> #include <functional> template<class Fun, class... Args> void foo(Fun f, Args... args) { f(args...); } int main() { int a = 2; int b = 1000; foo([](int &b, int a){ b = a; }, std::ref(b), a); std::cout << b << std::endl; }
Source: https://habr.com/ru/post/907434/More articles:https://translate.googleusercontent.com/translate_c?depth=1&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/907429/why-am-i-getting-the-no-namespaces-to-aot-compile-listed-in-projectclj-warning&usg=ALkJrhg2zJS5somihAU2roKJNZOj8IIrnAweb2py insertion methods - pythonMixing custom Google Maps overlays with Backbone Views - prototypal-inheritanceWhy does C # Convert.ToBase64String () give me 88 as length when I pass 64 bytes? - c #Pretty printers for cards throwing an error like - c ++using typeid to compare between derived classes - c ++How to embed Google docs collection in iframe? - iframehttps://translate.googleusercontent.com/translate_c?depth=1&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/907437/adding-tracking-parameters-to-links-from-facebook-timeline-actions&usg=ALkJrhiS_4kbAgMrpcf3NfXyZCH0FvMVJQCaptures assertions in NUnit - .netAlarmManager with telephone connection - androidAll Articles