pass-by-value

why function objects should be pass-by-value

不问归期 提交于 2019-11-27 01:26:57
问题 I have just read the classic book "Effective C++, 3rd Edition", and in item 20 the author concludes that built-in types, STL iterators and function object types are more appropriate for pass-by-value . I could well understand the reason for built-in and iterators types, but why should the function object be pass-by-value , as we know it is class-type anyway? 回答1: In a typical case, a function object will have little or (more often) no persistent state. In such a case, passing by value may no

Is the pass-by-value-and-then-move construct a bad idiom?

ⅰ亾dé卋堺 提交于 2019-11-27 00:02:33
Since we have move semantics in C++, nowadays it is usual to do void set_a(A a) { _a = std::move(a); } The reasoning is that if a is an rvalue, the copy will be elided and there will be just one move. But what happens if a is an lvalue? It seems there will be a copy construction and then a move assignment (assuming A has a proper move assignment operator). Move assignments can be costly if the object has too many member variables. On the other hand, if we do void set_a(const A& a) { _a = a; } There will be just one copy assignment. Can we say this way is preferred over the pass-by-value idiom

Pass by reference more expensive than pass by value

橙三吉。 提交于 2019-11-26 23:09:50
问题 Is there a case where pass-by-reference is more expensive than pass-by-value in C++? If so, what would that case be? 回答1: Prefer passing primitive types (int, char, float, ...) and POD structs that are cheap to copy (Point, complex) by value. This will be more efficient than the indirection required when passing by reference. See Boost's Call Traits. The template class call_traits<T> encapsulates the "best" method to pass a parameter of some type T to or from a function, and consists of a

Const correctness for value parameters

我与影子孤独终老i 提交于 2019-11-26 22:32:18
I know there are few question about const correctness where it is stated that the declaration of a function and its definition do not need to agree for value parameters. This is because the constness of a value parameter only matters inside the function. This is fine: // header int func(int i); // cpp int func(const int i) { return i; } Is doing this really a best practice? Because I've never seen anyone do it. I've seen this quotation (not sure of the source) in other places this has been discussed: "In fact, to the compiler, the function signature is the same whether you include this const

Function Overloading Based on Value vs. Const Reference

放肆的年华 提交于 2019-11-26 22:32:01
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. Alexander Gessler The intent seems to be to differenciate between invocations with temporaries (i.e. 9 ) and 'regular' argument passing. The first case may allow the function implementation to employ

Python : When is a variable passed by reference and when by value? [duplicate]

孤街醉人 提交于 2019-11-26 21:51:19
Possible Duplicate: Python: How do I pass a variable by reference? My code : locs = [ [1], [2] ] for loc in locs: loc = [] print locs # prints => [ [1], [2] ] Why is loc not reference of elements of locs ? Python : Everything is passed as reference unless explicitly copied [ Is this not True ? ] Please explain.. how does python decides referencing and copying ? Update : How to do ? def compute(ob): if isinstance(ob,list): return process_list(ob) if isinstance(ob,dict): return process_dict(ob) for loc in locs: loc = compute(loc) # What to change here to make loc a reference of actual locs

Where should I prefer pass-by-reference or pass-by-value?

流过昼夜 提交于 2019-11-26 20:35:53
In what circumstances should I prefer pass-by-reference? Pass-by-value? There are five main cases where you should use pass-by-reference over pass-by-value: If you are calling a function that needs to modify its arguments, use pass-by-reference as its the only way to get this effect (I treat pass-by-reference and pass-by-pointer interchangeably in this case, though with pass-by-pointer you often have to explicitly check for NULL). If you're calling a function that needs to take a large object as a parameter, pass it by const reference to avoid making an unnecessary copy of that object and

Performance cost of passing by value vs. by reference or by pointer?

拟墨画扇 提交于 2019-11-26 20:22:45
问题 Let's consider an object foo (which may be an int , a double , a custom struct , a class , whatever). My understanding is that passing foo by reference to a function (or just passing a pointer to foo ) leads to higher performance since we avoid making a local copy (which could be expensive if foo is large). However, from the answer here it seems that pointers on a 64-bit system can be expected in practice to have a size of 8 bytes, regardless of what's being pointed. On my system, a float is

Java, pass-by-value, reference variables

帅比萌擦擦* 提交于 2019-11-26 19:07:30
I have a problem with understanding the "pass-by-value" action of Java in the following example: public class Numbers { static int[] s_ccc = {7}; static int[] t_ccc = {7}; public static void calculate(int[] b, int[] c) { System.out.println("s_ccc[0] = " + s_ccc[0]); // 7 System.out.println("t_ccc[0] = " + t_ccc[0]); // 7 b[0] = b[0] + 9; System.out.println("\nb[0] = " + b[0]); // 16 c = b; System.out.println("c[0] = " + c[0] + "\n"); // 16 } public static void main(String[] args) { calculate(s_ccc, t_ccc); System.out.println("s_ccc[0] = " + s_ccc[0]); // 16 System.out.println("t_ccc[0] = " + t

python pandas dataframe, is it pass-by-value or pass-by-reference

假装没事ソ 提交于 2019-11-26 18:56:26
问题 If I pass a dataframe to a function and modify it inside the function, is it pass-by-value or pass-by-reference? I run the following code a = pd.DataFrame({'a':[1,2], 'b':[3,4]}) def letgo(df): df = df.drop('b',axis=1) letgo(a) the value of a does not change after the function call. Does it mean it is pass-by-value? I also tried the following xx = np.array([[1,2], [3,4]]) def letgo2(x): x[1,1] = 100 def letgo3(x): x = np.array([[3,3],[3,3]]) It turns out letgo2() does change xx and letgo3()