Why does type-deduction for arrays pause a pointer to the first array reference?

int v[1]; auto p1 = v; auto &p2 = v; auto *p3 = v; 

p1 is of type int * (the same for p3 ). In particular, in this trivial example, I find p2 ( int (&)[1] ) more useful, since it is inherent in the semantics of the array, for example. I can apply sizeof on p2 to give the same thing as sizeof on v .

Is there a standard quote regarding this?

Why is link rejection a bad idea? (for this case of arrays, I mean that almost no C ++ programmer cares about them these days anyway ...)

+7
c ++ c ++ 11 templates auto type-deduction
source share
2 answers

auto prints a non reference type.

auto& displays a link.

auto const& displays a const link.

auto&& displays a link, a const link, or an rvalue link.

This works the same way that type inference works when the template function is called.

 template<typename T> void foo( T t ); 

T will never be inferred as a reference type - it will always be the value type in the output.

auto follows almost the same rules:

 template<typename T> void foo( T&& t ); 

- a fairly well-known "universal link", similar to a variable of the type auto&& .

+4
source share

I believe to be consistent with functions without templates. Arrays are converted between arrays and pointers at any time, unless they are bound to a reference. Thus, with existing rules, the following are followed:

 int v[1]; void foo(int *); int main() { foo(v); } 

and

 int v[1]; template <class T> void foo(T); int main() { foo(v); } 
+2
source share

All Articles