C++中数组的大小(SizeOfArray)
来源:互联网 发布:js正则表达式实例 编辑:程序博客网 时间:2024/05/18 02:12
////Loki Start////////////////////////////////////////////////////////////////////////////////// The Loki Library// Copyright (c) 2001 by Andrei Alexandrescu// This code accompanies the book:// Alexandrescu, Andrei. "Modern C++ Design: Generic Programming and Design // Patterns Applied". Copyright (c) 2001. Addison-Wesley.// Permission to use, copy, modify, distribute and sell this software for any // purpose is hereby granted without fee, provided that the above copyright // notice appear in all copies and that both that copyright notice and this // permission notice appear in supporting documentation.// The author or Addison-Wesley Longman make no representations about the // suitability of this software for any purpose. It is provided "as is" // without express or implied warranty.////////////////////////////////////////////////////////////////////////////////template<int> struct CompileTimeError;template<> struct CompileTimeError<true> {};/** 取自Loki, 静态断言* 比如用以下语句来确认size_t类型可以准确存储一个指针* STATIC_ASSERT(sizeof(size_t) == sizeof(void*), SIZE_T_LENGTH_NOT_SUITABLE);*/#define STATIC_ASSERT(expr, msg) \{ CompileTimeError<((expr) != 0)> ERROR_##msg; (void)ERROR_##msg; }////Loki Endnamespace InnerTypeTraits{ // bool_type, 用于返回一个常量 template<bool b_> struct bool_type{ static const bool value = b_; }; template<bool b_> const bool bool_type<b_>::value; // is_array<T>, 判断T是否是数组 template<typename T> struct is_array : bool_type<false>{ }; template<typename T> struct is_array<T[]>: bool_type<true>{ }; template<typename T, unsigned int N> struct is_array<T[N]> : bool_type<true>{ };}// START SizeOfArray 定义// 作用:判定数组元素的个数// 优点:如果误传普通指针,则编译报错template<typename T>size_t SizeOfArray(T& t){ STATIC_ASSERT(InnerSafePtr::InnerTypeTraits::is_array<T>::value, typeNotArray); return sizeof(t)/sizeof(t[0]);}
0 0
- C++中数组的大小(SizeOfArray)
- C语言中字符数组的大小
- c中数组大小不能用变量的一个解决办法
- C语言中const分析(未知大小的全局变量数组声明出错)
- C++STL中vector(可变大小数组)用法总结
- c大小为0的数组
- Linux c 数组大小的限制
- C中,在struct中,得到一个可变大小的数组
- c指针、数组(计算大小)
- C/C++中求字符串数组的大小---sizeof,strlen,string,length()
- Fortran中可变大小数组的定义
- Python中数组及矩阵的大小
- C语言中怎么求动态数组大小
- 一起talk C栗子吧(第九十 一回:C语言实例--数组的大小)
- 关于C中结构体的大小问题(sizeof)
- 在C++中怎样获得数组的大小?(原文标题:How Would You Get the Count of an Array in C++?)
- C中结构体大小的计算
- c语言中大小端的问题
- CSS3 Transform
- k-means的分类数目
- opencv 记录
- 【遇见大咖】软件测试的前途与职业发展
- c4d导出obj结合keyshot5渲染破面问题
- C++中数组的大小(SizeOfArray)
- Qt编写串口通信程序全程图文讲解
- JQuery中根据属性或属性值获得元素(6种情况获取方法)
- poj3411(状态压缩dp,dijkstra最短路)
- Java%(取模运算)详解
- 艺术编程-技术之声第四期
- Gcc的编译步骤
- 沃森计算机显能耐,微型气象预报有戏
- 使用Lua脚本对pcap文件进行切流操作