Array decay to pointers in templates

后端 未结 4 1748
感情败类
感情败类 2020-12-09 23:32

Please consider this code:

#include 

template
void f(T x) {
    std::cout << sizeof(T) << \'\\n\';
}

int main         


        
4条回答
  •  情深已故
    2020-12-10 00:10

    You can also use templates like the following:

    template 
    inline std::size_t number_of_elements(T (&ary)[N]) {
        return N;
    }
    

    This little trick will cause compile errors if the function is used on a non-array type.

提交回复
热议问题