3 ms·
My point was simply that auto to assign pointers doesn't cause them to lose their pointiness, but using auto to assign a reference causes them to lose their ref
by overgard 5y ago
My point was simply that auto to assign pointers doesn't cause them to lose their pointiness, but using auto to assign a reference causes them to lose their referenceness. Yes I understand why it works the way it does. I'm not saying it's wrong just that it's been an occasional footgun.
- Thorrez 5y agoI think it would be a footgun the other way too. I tend to think of pointer a as a general type aspect whereas reference is specifically on the variable that it's on and doesn't transfer. Sort or like how auto doesn't copy the static specifier. Also the rules for auto are the same as for template argument deduction. template<typename T> std::vector<T> MakeVec(T val) { std::vector<T> vec; vec.push_back(val); return vec; } int main() { int x = 1; int& y = x; std::vector<int> vec = MakeVec(y); } Notice how in this case MakeVec(y) makes a std::vector<int> not a std::vector<int&>. If you want your template argument function to take a reference, use T& not T (or use T&& which is a whole new complication). Similarly use auto& not auto.
- einpoklum 5y agoOk, that's a fair point. It's easy to make the distinction when you think about how in C++, things become references or lose their reference'iness pretty easily, but nothing ever implicitly becomes a pointer.
- jdashg 5y agoReference is a type qualifier, pointer is a type. Best to just internalize it, even if you'd rather it be different.