reference

What is the difference between &Trait and impl Trait when used as method arguments?

那年仲夏 提交于 2019-12-22 04:52:08
问题 In my project so far, I use many traits to permit mocking/stubbing in unit tests for injected dependencies. However, one detail of what I'm doing so far seems so suspicious that I'm surprised it even compiles. I'm worried that something dangerous is going on that I don't see or understand. It's based on the difference between these two method signatures: fn confirm<T>(subject: &MyTrait<T>) ... fn confirm<T>(subject: impl MyTrait<T>) ... I only just discovered the impl ... syntax in method

Do mainstream compilers convert passed-by-reference basic types into pass-by-copy?

大憨熊 提交于 2019-12-22 04:43:41
问题 Passing an object by reference is an easier, faster and safer way to pass an address to it. But for most compilers, it's all the same: references are really pointers. Now what about basic types like int ? Passing an address to an int and using it inside a function would be slower than passing it by copy, because the pointer needs to be dereferenced before use. How do modern compiler handle, this? int foo(const int & i) { cout << i; // Do whatever read-only with i. } May I trust them to

Reference initialization in C++

筅森魡賤 提交于 2019-12-22 04:26:34
问题 Can anybody explain to me why there is a difference between these two statements? class A{}; const A& a = A(); // correct A& b = A(); // wrong It says invalid initialization of non-const reference of type A& from a temporary of type A Why does const matter here? 回答1: Non-const references must be initialised with l-values. If you could initialise them with temporaries, then what would the following do? int& foo = 5; foo = 6; // ?! const references have the special property that they extend the

How to manage version dependencies in a C#/.NET Project?

寵の児 提交于 2019-12-22 04:16:11
问题 Suppose you have Project A, and this has various dll dependencies, but the tree looks something like this: Project A => Project B => Project C => Project D => Project C => Project E => Project C, v2 Is there a way to use Project E since it relies on a newer version of Project C (dll) even though the rest of the project is using an older version of that same library? If there is not a way, or one that would cause much gnashing of teeth, is there a forward thinking solution to preventing this

Why are we allowed to take the address of an incomplete type?

这一生的挚爱 提交于 2019-12-22 04:04:30
问题 Consider this code: class Addressable; class Class1 { void foo(Addressable &a) { (void) &a; } }; // OK class Addressable { void *operator &() { return this; } }; class Class2 { void foo(Addressable &a) { (void) &a; } }; // Error: operator & private Why does C++ allow taking the address of an incomplete reference type? Couldn't it be potentially illegal, as shown above? Is this intentional? 回答1: Yes, that's intentional, and the possibility of breakage if operator& is overloaded is known.

Google's style guide about input/output parameters as pointers

╄→尐↘猪︶ㄣ 提交于 2019-12-22 03:51:16
问题 The Google C++ Style Guide draws a clear distinction (strictly followed by cpplint.py) between input parameters(→ const ref, value) and input-output or output parameters (→ non const pointers) : Parameters to C/C++ functions are either input to the function, output from the function, or both. Input parameters are usually values or const references, while output and input/output parameters will be non-const pointers. And further : In fact it is a very strong convention in Google code that

Approximate Number of CPU Cycles for Various Operations

不羁的心 提交于 2019-12-22 03:50:55
问题 I am trying to find a reference for approximately how many CPU cycles various operations require. I don't need exact numbers (as this is going to vary between CPUs) but I'd like something relatively credible that gives ballpark figures that I could cite in discussion with friends. As an example, we all know that floating point division takes more CPU cycles than say doing a bitshift. I'd guess that the difference is that the division is around 100 cycles, where as a shift is 1 but I'm looking

pass array by reference in java

狂风中的少年 提交于 2019-12-22 03:25:12
问题 Is it ok to pass an array as an argument in java like this int[5] &result ? I want to pass the reference to the array because I want to change the array in the calling function. What would be the syntax? 回答1: private void demo() { int[] array = new int[5]; System.out.println(Arrays.toString(array)); // 0, 0, 0, 0, 0 fillArray(array); System.out.println(Arrays.toString(array)); // 0, 1, 2, 3, 4 } private void fillArray(int[] array) { for (int i = 0; i < array.length; i++) { array[i] = i; } }

“An assembly with the same simple name has already been imported” error without duplicate reference

随声附和 提交于 2019-12-22 03:18:14
问题 I'm getting the following error: error CS1704: An assembly with the same simple name 'Interop.xxx.dll, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null has already been imported. Try removing one of the references or sign them to enable side-by-side. Everything I've seen says that I am referencing two assemblies with the same name and I need to remove one of them. However, I've checked and I'm only referencing it once. This also only happens when I'm using msbuild to build from the

Is there a rational explanation for this PHP call-by-value behavior? Or PHP bug?

对着背影说爱祢 提交于 2019-12-22 02:05:23
问题 PHP 5.5.12. Consider this: <?php $a = [ 'a', 'b', 'c' ]; foreach($a as &$x) { $x .= 'q'; } print_r($a); This, as expected, outputs: Array ( [0] => aq [1] => bq [2] => cq ) Now consider: <?php $a = [ 'a', 'b', 'c' ]; foreach(z($a) as &$x) { $x .= 'q'; } print_r($a); function z($a) { return $a; } This outputs: Array ( [0] => aq [1] => bq [2] => cq ) (!) But wait a minute. $a is not being passed by reference. Which means I should be getting a copy back from z(), which would be modified, and $a