ref

Is this std::ref behaviour logical?

雨燕双飞 提交于 2019-12-03 01:58:39
Consider this code: #include <iostream> #include <functional> int xx = 7; template<class T> void f1(T arg) { arg += xx; } template<class T> void f2(T arg) { arg = xx; } int main() { int j; j=100; f1(std::ref(j)); std::cout << j << std::endl; j=100; f2(std::ref(j)); std::cout << j << std::endl; } When executed, this code outputs 107 100 I would have expected the second value to be 7 rather than 100. What am I missing? A small modification to f2 provides the clue: template<class T> void f2(T arg) { arg.get() = xx; } This now does what you expect. This has happened because std::ref returns a std:

What would be a “Hello, World!” example for “std::ref”?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-03 01:10:20
问题 Can somebody give a simple example which demonstrates the functionality of std::ref ? I mean an example in which some other constructs (like tuples, or data type templates) are used only if it is impossible to explain std::ref without them. I found two questions about std::ref here and here. But in the first one it goes about a bug in a compiler and in the second one, examples of use of std::ref do not contain std::ref and they involve tuples and data type templates which make understanding

C++ Difference between std::ref(T) and T&?

假如想象 提交于 2019-12-03 00:59:01
问题 I have some questions regarding this program: #include <iostream> #include <type_traits> #include <functional> using namespace std; template <typename T> void foo ( T x ) { auto r=ref(x); cout<<boolalpha; cout<<is_same<T&,decltype(r)>::value; } int main() { int x=5; foo (x); return 0; } The output is: false I want to know, if std::ref doesn't return the reference of an object, then what does it do? Basically, what is the difference between: T x; auto r = ref(x); and T x; T &y = x; Also, I

C# ref is it like a pointer in C/C++ or a reference in C++?

ε祈祈猫儿з 提交于 2019-12-03 00:40:35
问题 I'm working with the ref and don't understand clearly "Is it like a pointer as in C/C++ or it's like a reference in C++?" Why did I ask such a weak question as you thought for a moment? Because, when I'm reading C#/.NET books, msdn or talking to C# developers I'm becoming confused by the following reasons: C# developers suggest NOT to use ref in the arguments of a function, e.g. ...(ref Type someObject) doesn't smell good for them and they suggest ...(Type someObject) , I really don't

Why can't iterator methods take either 'ref' or 'out' parameters?

风流意气都作罢 提交于 2019-12-02 17:02:49
I tried this earlier today: public interface IFoo { IEnumerable<int> GetItems_A( ref int somethingElse ); IEnumerable<int> GetItems_B( ref int somethingElse ); } public class Bar : IFoo { public IEnumerable<int> GetItems_A( ref int somethingElse ) { // Ok... } public IEnumerable<int> GetItems_B( ref int somethingElse ) { yield return 7; // CS1623: Iterators cannot have ref or out parameters } } What's the rationale behind this? C# iterators are state machines internally. Every time you yield return something, the place where you left off should be saved along with the state of local variables

C++ Difference between std::ref(T) and T&?

余生颓废 提交于 2019-12-02 16:20:13
I have some questions regarding this program: #include <iostream> #include <type_traits> #include <functional> using namespace std; template <typename T> void foo ( T x ) { auto r=ref(x); cout<<boolalpha; cout<<is_same<T&,decltype(r)>::value; } int main() { int x=5; foo (x); return 0; } The output is: false I want to know, if std::ref doesn't return the reference of an object, then what does it do? Basically, what is the difference between: T x; auto r = ref(x); and T x; T &y = x; Also, I want to know why does this difference exist? Why do we need std::ref or std::reference_wrapper when we

In React do ref's reference the virtual DOM, or the actual DOM?

╄→尐↘猪︶ㄣ 提交于 2019-12-02 06:58:17
I'm assuming the virtual DOM, and that React takes care of it with diff'ing. But I had a recruiter say that ref's affect the actual DOM, I can't see how this can be. I assume that they were just mistaken. Refs should reference the actual DOM. One usage of Refs is integrating with third-party DOM libraries, so you can directly modify the DOM using Refs. If Refs reference the virtual DOM, I don't think the demand can be meet. You modify a virtual DOM, but you can't make sure the modification would be synchronized to the actual DOM. Besides, if you want to modify actual DOM when using react, you

Passing List<int> by ref [duplicate]

会有一股神秘感。 提交于 2019-12-02 00:44:05
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: passing in object by ref With the code below, the output would be: Without: With:1 Code: static void Main(string[] args) { var listWithoutRef = new List<int>(); WithoutRef(listWithoutRef); Console.WriteLine("Without:" + string.Join(" ", listWithoutRef)); var listWithRef = new List<int>(); WithRef(ref listWithRef); Console.WriteLine("With:" + string.Join(" ", listWithRef)); } static void WithoutRef(List<int>

passing in object by ref

我是研究僧i 提交于 2019-12-01 22:01:45
What is the difference between public function Foo(ref Bar bar) { bar.Prop = 1; } public function Foo(Bar bar) { bar.Prop = 1; } essentially what is the point of "ref". isn't an object always by reference? The point is that you never actually pass an object . You pass a reference - and the argument itself can be passed by reference or value. They behave differently if you change the parameter value itself, e.g. setting it to null or to a different reference. With ref this change affects the caller's variable; without ref it was only a copy of the value which was passed, so the caller doesn't

Recover containing GC object from managed 'ref' interior pointer

丶灬走出姿态 提交于 2019-12-01 21:25:19
问题 This question is newly relevant in light of the new ref locals and ref return features in the latest versions of C# 7 : With the increased prominence and wider use of managed--or "interior"--pointer variables in C#, occasionally you may need to recover the respective containing Pinnable GC object for such a pointer. For example, if you are passing around a managed pointer to an array element of type T , you might need the array reference T[] itself in order to call (e.g.) Array.Copy(...) . So