pass-by-reference

__invoke() on callable Array or String

ε祈祈猫儿з 提交于 2019-12-31 03:17:07
问题 How would one write PHP code to call all "Callables" with __invoke() ? The desire here is pass by reference, which is deprecated with call_user_func[_array]() . I did see that there is a package out there, TRex\Reflection\CallableReflection , but this seems to utilize call_user_func() in the background, and would suffer the same issue. <?php function passthrough_invoke(callable $callback) { return $callback->__invoke(); } function passthrough_user(callable $callback) { return call_user_func(

Pass int by const reference or by value , any difference? [duplicate]

て烟熏妆下的殇ゞ 提交于 2019-12-30 09:10:20
问题 This question already has answers here : Is it counter-productive to pass primitive types by reference? [duplicate] (7 answers) Closed 4 years ago . When I pass primitives like int and double to functions , is it better to pass them by const reference , or by value (assuming that I don't change the variable's value) ? int getValueFromArray(int index) { // return the value from the array } int getValueFromArray(const int& index) { // return the value from the array } Thanks 回答1: For primitive

Pass int by const reference or by value , any difference? [duplicate]

我只是一个虾纸丫 提交于 2019-12-30 09:10:09
问题 This question already has answers here : Is it counter-productive to pass primitive types by reference? [duplicate] (7 answers) Closed 4 years ago . When I pass primitives like int and double to functions , is it better to pass them by const reference , or by value (assuming that I don't change the variable's value) ? int getValueFromArray(int index) { // return the value from the array } int getValueFromArray(const int& index) { // return the value from the array } Thanks 回答1: For primitive

javascript return reference to array item

点点圈 提交于 2019-12-30 01:59:27
问题 I have a array like this: users = [{id:1, name:'name1'},{id:2, name:'name2'}] How could I get a reference to the item {id:2, name:'name2'}, so I can change it is name property, like: user = get_item(users, 'id', 2); user.name = "user2 name changed"; console.log(users) will have the result: [{id:1, name:'name1'},{id:2, name:'user2 name changed'}] I tried to use Array.filter() function, but it returns a new array instead of a reference to the original array. which I can not use to mutate the

Pass by value or reference, to a C++ constructor that needs to store a copy?

僤鯓⒐⒋嵵緔 提交于 2019-12-29 01:37:14
问题 Should a C++ (implicit or explicit) value constructor accept its parameter(s) by value or reference-to-const, when it needs to store a copy of the argument(s) in its object either way? Here is the shortest example I can think of: struct foo { bar _b; foo(bar [const&] b) // pass by value or reference-to-const? : _b(b) { } }; The idea here is that I want to minimize the calls to bar's copy constructor when a foo object is created, in any of the various ways in which a foo object might get

:= (pass by reference) operator in the data.table package modifies another data table object simultaneously

久未见 提交于 2019-12-28 03:01:06
问题 While testing my code, I found out the following: If I assign a data.table DT1 to DT and change DT afterwards, DT1 changes with it. So DT and DT1 seem to be internally linked. Is this intended behavior? Although I'm not a programming expert, this looks wrong to me, and testing it with simple R variables or a data.frame , I couldn't reproduce the behavior. What's happening here? DF <- data.frame(ID=letters[1:5], value=1:5) DF1 <- DF all.equal(DF1, DF) [1] TRUE DF[1, "value"] <- DF[1, "value"]

modify variable within R function

泪湿孤枕 提交于 2019-12-28 02:06:27
问题 How do I modify an argument being passed to a function in R? In C++ this would be pass by reference. g=4 abc <- function(x) {x<-5} abc(g) I would like g to be set to 5. 回答1: There are ways as @Dason showed, but really - you shouldn't ! The whole paradigm of R is to "pass by value". @Rory just posted the normal way to handle it - just return the modified value... Environments are typically the only objects that can be passed by reference in R. But lately new objects called reference classes

Function Overloading Based on Value vs. Const Reference

泄露秘密 提交于 2019-12-27 17:40:52
问题 Does declaring something like the following void foo(int x) { std::cout << "foo(int)" << std::endl; } void foo(const int &x) { std::cout << "foo(const int &)" << std::endl; } ever make sense? How would the caller be able to differentiate between them? I've tried foo(9); // Compiler complains ambiguous call. int x = 9; foo(x); // Also ambiguous. const int &y = x; foo(y); // Also ambiguous. 回答1: The intent seems to be to differenciate between invocations with temporaries (i.e. 9 ) and 'regular'

In PHP (>= 5.0), is passing by reference faster?

会有一股神秘感。 提交于 2019-12-27 10:40:03
问题 In PHP, function parameters can be passed by reference by prepending an ampersand to the parameter in the function declaration, like so: function foo(&$bar) { // ... } Now, I am aware that this is not designed to improve performance, but to allow functions to change variables that are normally out of their scope. Instead, PHP seems to use Copy On Write to avoid copying objects (and maybe also arrays) until they are changed. So, for functions that do not change their parameters, the effect

In PHP (>= 5.0), is passing by reference faster?

戏子无情 提交于 2019-12-27 10:39:07
问题 In PHP, function parameters can be passed by reference by prepending an ampersand to the parameter in the function declaration, like so: function foo(&$bar) { // ... } Now, I am aware that this is not designed to improve performance, but to allow functions to change variables that are normally out of their scope. Instead, PHP seems to use Copy On Write to avoid copying objects (and maybe also arrays) until they are changed. So, for functions that do not change their parameters, the effect