forked from charlybigoud/tardis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
memory_alignement.cpp
65 lines (48 loc) · 1.61 KB
/
memory_alignement.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
#include <cassert>
#include <array>
template<typename T, std::size_t S>
struct AlignedArray{
typename std::aligned_storage<sizeof(T), alignof(T)>::type data[S];
std::size_t m_size = 0;
// AlignedArray(const T& ... t)
// {
// }
// Delete objects from aligned storage
~AlignedArray()
{
for (std::size_t i = 0; i < m_size; ++i)
reinterpret_cast<T*>(data + i)->~T();
}
T& operator[](const std::size_t i) { assert(S>i); return *reinterpret_cast< T*>(data + i); }
const T& operator[](const std::size_t i) const { assert(S>i); return *reinterpret_cast<const T*>(data + i); }
};
// template<int DIMENSION> NodeGenerator<DIMENSION> begin(const GridMesh<DIMENSION>& gm){return NodeGenerator<DIMENSION>{gm, 0}; };
// template<int DIMENSION> NodeGenerator<DIMENSION> end(const GridMesh<DIMENSION>& gm){return NodeGenerator<DIMENSION>{gm, gm.nodeNbr()}; };
template<typename T, std::size_t S>
std::ostream& operator<<(std::ostream& o, const AlignedArray<T,S>& array)
{
// for (auto& a : array)
// o << a;
for (size_t i = 0; i<S; ++i)
o << array[i] << ", ";
return o;
}
int main()
{
AlignedArray<int, 4> a;
std::cout << a << std::endl;
a[0] = 45;
a[1] = 10;
a[2] = 20;
a[3] = 30;
for (std::size_t i = 0; i < 4; ++i)
std::cout << a[i] << ", ";
std::cout << std::endl;
std::cout << a << std::endl;
// AlignedArray<int, 4> b{1,2,3,4};
// std::cout << b << std::endl;
// AlignedArray<int, 4> c(1,2,3,4);
// std::cout << c << std::endl;
return 0;
}