数组明确衰变成一个指针 [英] Explicit decay of an array into a pointer

查看:127
本文介绍了数组明确衰变成一个指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是最简洁和习惯的方法的明确的腐朽数组的指针?


例如,请考虑您需要能够引导SFINAE或更明确一些超载的情况:

 模板< typename的T,性病::为size_t N'GT;无效美孚(T(安培; X)[N]);
模板< typename的T>无效美孚(T * X);
//
INT X [2] = {0,1};
富(X);


解决方案

您可以使用以下之一:

 富(X + 0);
富(+ X);
富(安培; X [0]); //假设运营商和放大器;一直没有超载对于T
美孚(性病:: addressof(X [0])); //因为C ++ 11

What is the most concise and idiomatic way of explicitly decaying an array into a pointer?


For example, consider the case where you need to be able to guide SFINAE or be explicit about an overload:

template<typename T, std::size_t N> void foo(T(&x)[N]);
template<typename T>                void foo(T *x); 
//
int x[2] = {0, 1};
foo(x);

解决方案

You may use one of the following:

foo(x + 0);
foo(+x);
foo(&x[0]); // assuming operator& has not been overloaded for T
foo(std::addressof(x[0])); // since C++11

这篇关于数组明确衰变成一个指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆