source: cpp/common/nonstd_stl.h @ 1082

Last change on this file since 1082 was 1027, checked in by Maciej Komosinski, 4 years ago

#defined ARRAY_LENGTH(x) replaced by std::size(x) since C++17

  • Property svn:eol-style set to native
File size: 1.8 KB
RevLine 
[286]1// This file is a part of Framsticks SDK.  http://www.framsticks.com/
2// Copyright (C) 1999-2015  Maciej Komosinski and Szymon Ulatowski.
3// See LICENSE.txt for details.
[122]4
[109]5#ifndef _NONSTD_STL_H_
6#define _NONSTD_STL_H_
7
8//stl jak sama nazwa glosi wcale nie jest nonstd
9
10#include <string>
11using std::string;
12#ifndef SHP //bada nie ma wstring
13using std::wstring;
14#endif
15
16#include <vector>
17using std::vector;
18
19#include <algorithm> //std::min,max,swap
20using std::min;
21using std::max;
22using std::swap;
23
24
25// ------------------- ARRAY_LENGTH -------------------
26
27//staromodne makro, niezabezpieczone przed uzyciem wskaznika w roli "x"
28//#define ARRAY_LENGTH(x) (sizeof(x)/sizeof((x)[0]))
29
30//hakerskie makro ktore wykrywa czesc pomy³kowych przypadkow uzycia
31//#define ARRAY_LENGTH(x) ((sizeof(x)/sizeof(0[x])) / ((size_t)(!(sizeof(x) % sizeof(0[x])))))
32
33//szablonowa funkcja pisana przez sredniozaawansowanych, jak to funkcja - nie daje niestety sta³ej w czasie kompilacji
34//template<typename T, std::size_t N> inline std::size_t ARRAY_LENGTH( T(&)[N] ) { return N; } //"constexpr" dopiero w C++0x
35
36//szablony hakerskie: tablica bajtow o dlugosci N - tak dluga jak tablica o któr¹ pytamy...
[1027]37//template <typename T, std::size_t N>
38//char (&array_temp(T (&a)[N]))[N];
[109]39
40// As litb noted in comments, you need this overload to handle array rvalues
41// correctly (e.g. when array is a member of a struct returned from function),
42// since they won't bind to non-const reference in the overload above.
[1027]43//template <typename T, std::size_t N>
44//char (&array_temp(const T (&a)[N]))[N];
[109]45
46//...ktor¹ mozna potem uzyc normalnie w sizeof i dzieki temu mamy const w compile-time. tak uzyteczne jak staromodne makro ale z pelna kontrola bledow
[1027]47//#define ARRAY_LENGTH(x) sizeof(array_temp(x))
[109]48
[1027]49//final and no longer needed version ;-) (c++17)
50#define ARRAY_LENGTH(x) std::size(x)
[109]51
[1027]52
[109]53#endif
Note: See TracBrowser for help on using the repository browser.