Skip to content

Commit f84ad97

Browse files
committed
std::array
1 parent bfa699d commit f84ad97

4 files changed

Lines changed: 21 additions & 25 deletions

File tree

‎cpp/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
1. [Headers](common.hpp)
6565
1. [Containers](containers.md)
6666
1. [vector](vector.cpp)
67+
1. [array](array.cpp)
6768
1. [string](string.cpp)
6869
1. [list](list.cpp)
6970
1. [map](map.cpp)

‎cpp/array.cpp‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/* https://stackoverflow.com/questions/4424579/stdvector-versus-stdarray-in-c */
2+
3+
#include "common.hpp"
4+
5+
int main() {
6+
std::array<int, 3> a{{0, 1, 2}};
7+
assert(a.size() == 3);
8+
// No implicit bounds checks.
9+
//std::cout << a[4] << std::endl;
10+
}

‎cpp/main.cpp‎

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -109,30 +109,6 @@ int main() {
109109
compile time constants.
110110
*/
111111

112-
/*
113-
Get array length, find array length: http://stackoverflow.com/questions/4108313/how-do-i-find-the-length-of-an-array
114-
115-
Besides the C sizeof method which does not work for pointers and has lots of duplication, C++ offers:
116-
*/
117-
{
118-
// Vector class. Has overhead, but probably the best option given its flexibility.
119-
120-
// #array class. No overhead. Fixed size. Template based.
121-
{
122-
// WARN: double brackets required TODO why
123-
//std::array<int, 3> a{0, 1, 2};
124-
125-
std::array<int, 3> a{{0, 1, 2}};
126-
assert(a.size() == 3);
127-
}
128-
129-
// Method which I don't understand.
130-
{
131-
//template<class T, size_t N>
132-
//size_t size(T (&)[N]) { return N; }
133-
}
134-
}
135-
136112
/*
137113
# array size from variables
138114

‎cpp/pod.cpp‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,12 +220,21 @@ int main() {
220220
}
221221

222222
/* Standard library containers are not, for the most part (all?),
223-
* POD because they are not trivial.
223+
* POD because they are not trivial, which can be seen directly from their
224+
* interface definition in the standard.
225+
* https://stackoverflow.com/questions/27165436/pod-implications-for-a-struct-which-holds-an-standard-library-container
224226
*/
225227
{
226228
static_assert(!std::is_pod<std::vector<int>>());
227229
static_assert(!std::is_trivially_copyable<std::vector<int>>());
228230
}
231+
232+
/* Array of POD is POD. */
233+
{
234+
struct C {};
235+
static_assert(std::is_pod<C>());
236+
static_assert(std::is_pod<C[]>());
237+
}
229238
}
230239
#endif
231240
}

0 commit comments

Comments
 (0)