why does the array decay to a pointer in a template function

前端 未结 5 902
抹茶落季
抹茶落季 2020-12-16 04:24

I don\'t understand why the array decays to a pointer in a template function.

If you look at the following code: When the parameter is forced to be a reference (fun

5条回答
  •  遥遥无期
    2020-12-16 04:56

    To quote from spec, it says

    (14.8.2.1/2) If P is not a reference type: — If A is an array type, the pointer type produced by the array-to-pointer standard conversion (4.2) is used in place of A for type deduction; otherwise

    So, in your case, It is clear that,

    template 
    void f1(T& buff) {
        std::cout << "f:buff size:" << sizeof(buff) << std::endl;       //prints 3
    }
    

    doesn't decay into pointer.

提交回复
热议问题