Last change
on this file since 97 was
82,
checked in by Maciej Komosinski, 12 years ago
|
compiles under more platforms and OSes
|
-
Property svn:eol-style set to
native
|
File size:
1.5 KB
|
Line | |
---|
1 | #ifndef _NONSTD_STL_H_ |
---|
2 | #define _NONSTD_STL_H_ |
---|
3 | |
---|
4 | //stl is not non-standard :-) |
---|
5 | |
---|
6 | #include <string> |
---|
7 | using std::string; |
---|
8 | |
---|
9 | #include <vector> |
---|
10 | using std::vector; |
---|
11 | |
---|
12 | #include <algorithm> //std::min,max,swap |
---|
13 | using std::min; |
---|
14 | using std::max; |
---|
15 | using std::swap; |
---|
16 | |
---|
17 | |
---|
18 | // ------------------- ARRAY_LENGTH ------------------- |
---|
19 | |
---|
20 | //staromodne makro, niezabezpieczone przed uzyciem wskaznika w roli "x" |
---|
21 | //#define ARRAY_LENGTH(x) (sizeof(x)/sizeof((x)[0])) |
---|
22 | |
---|
23 | //hakerskie makro ktore wykrywa czesc pomy³kowych przypadkow uzycia |
---|
24 | //#define ARRAY_LENGTH(x) ((sizeof(x)/sizeof(0[x])) / ((size_t)(!(sizeof(x) % sizeof(0[x]))))) |
---|
25 | |
---|
26 | //szablonowa funkcja pisana przez sredniozaawansowanych, jak to funkcja - nie daje niestety sta³ej w czasie kompilacji |
---|
27 | //template<typename T, std::size_t N> inline std::size_t ARRAY_LENGTH( T(&)[N] ) { return N; } //"constexpr" dopiero w C++0x |
---|
28 | |
---|
29 | //szablony hakerskie: tablica bajtow o dlugosci N - tak dluga jak tablica o któr¹ pytamy... |
---|
30 | template <typename T, std::size_t N> |
---|
31 | char (&array_temp(T (&a)[N]))[N]; |
---|
32 | |
---|
33 | // As litb noted in comments, you need this overload to handle array rvalues |
---|
34 | // correctly (e.g. when array is a member of a struct returned from function), |
---|
35 | // since they won't bind to non-const reference in the overload above. |
---|
36 | template <typename T, std::size_t N> |
---|
37 | char (&array_temp(const T (&a)[N]))[N]; |
---|
38 | |
---|
39 | //...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 |
---|
40 | #define ARRAY_LENGTH(x) sizeof(array_temp(x)) |
---|
41 | |
---|
42 | |
---|
43 | #endif |
---|
44 | |
---|
Note: See
TracBrowser
for help on using the repository browser.