C++ template specialization, calling methods on types that could be pointers or references unambiguously

Small overloaded functions can be used to turn reference into pointer: template<typename T> T * ptr(T & obj) { return &obj; } //turn reference into pointer! template<typename T> T * ptr(T * obj) { return obj; } //obj is already pointer, return it! Now instead of doing this: if(elem->Intersects(_bounds) == false) return false; if(elem.Intersects(_bounds) == … Read more

Is it Possible to Return a Reference to a Variable in C#? [duplicate]

UPDATE: The desired feature is now supported in C# 7. The CLR type system does support ref-returning methods, and I have written an experimental prototype of the C# compiler which supports the feature you want. (The prototype also implements ref-typed local variables, but ref-typed fields are illegal in the CLR type system.) You have hit … Read more

What lifetimes do I use to create Rust structs that reference each other cyclically?

It is not possible to create cyclic structures with borrowed pointers. There is not any good way of achieving cyclic data structures at present; the only real solutions are: Use reference counting with Rc<T> with a cyclic structure with Rc::new() and Rc:downgrade(). Read the rc module documentation and be careful to not create cyclic structures … Read more

Why do forwarding references have the same syntax as rvalue references?

I think it happened the other way around. The initial idea was to introduce rvalue-references into the language, meaning that “the code providing the double-ampersand reference does not care about what will happen to the referred-to object”. This permits move semantics. This is nice. Now. The standard forbids constructing a reference to a reference, but … Read more

What is the return type of the built-in assignment operator?

The standard correctly defines the return type of an assignment operator. Actually, the assignment operation itself doesn’t depend on the return value – that’s why the return type isn’t straightforward to understanding. The return type is important for chaining operations. Consider the following construction: a = b = c;. This should be equal to a … Read more

What are the reference collapsing rules, and how are they utilized by the C++ standard library?

The reference collapsing rules (save for A& & -> A&, which is C++98/03) exist for one reason: to allow perfect forwarding to work. “Perfect” forwarding means to effectively forward parameters as if the user had called the function directly (minus elision, which is broken by forwarding). There are three kinds of values the user could … Read more

What is the advantage of std::ref and std::reference_wrapper compared to regular references?

Well ref constructs an object of the appropriate reference_wrapper type to hold a reference to an object. Which means when you apply: auto r = ref(x); This returns a reference_wrapper and not a direct reference to x (ie T&). This reference_wrapper (ie r) instead holds T&. A reference_wrapper is very useful when you want to … Read more

Is it idiomatic to store references members in a class, and are there pitfalls?

Is there a name to describe this idiom? In UML it is called aggregation. It differs from composition in that the member object is not owned by the referring class. In C++ you can implement aggregation in two different ways, through references or pointers. I am assuming it is to prevent the possibly large overhead … Read more

Is a null reference possible?

References are not pointers. 8.3.2/1: A reference shall be initialized to refer to a valid object or function. [Note: in particular, a null reference cannot exist in a well-defined program, because the only way to create such a reference would be to bind it to the “object” obtained by dereferencing a null pointer, which causes … Read more