FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
cpp-is-magic/examples/example_1.cpp at main · MatthieuHernandez/cpp-is-magic · GitHub
MatthieuHernandez
/
cpp-is-magic
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
2
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
cpp-is-magic
/
examples
/
example_1.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
50 lines (39 loc) · 1.4 KB
Breadcrumbs
cpp-is-magic
/
examples
/
example_1.cpp
Copy path
File metadata and controls
50 lines (39 loc) · 1.4 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
#
include
<
array
>
#
include
<
string_view
>
template
<
typename
CT
,
size_t
S>
class
Tensor
{
using
T =
typename
std::remove_const<
CT
>::type;
public:
constexpr
Tensor
(
const
CT
* data =
nullptr
) {
if
(data !=
nullptr
) {
for
(
size_t
i =
0
; i < S; ++i) {
this
->
data
[i] = data[i];
}
}
}
consteval
const
T*
get_data
()
const
{
return
data; }
static
consteval
size_t
size
() {
return
S; }
template
<
typename
T2
,
size_t
S2
>
constexpr
auto
operator
+(
const
Tensor<
T2
,
S2
>& other)
const
{
static_assert
(S ==
S2
,
"
Tensors must be the same size to be added.
"
);
std::array<T, S> sum{};
for
(
size_t
i =
0
; i <
this
->
size
(); ++i) {
sum[i] =
this
->
data
[i] + other.
get_data
()[i];
}
return
Tensor<T, S>(sum.
data
());
}
consteval
std::string_view
dump
()
const
{
return
std::string_view
(data, S -
1
); }
private:
T data[S] = {};
};
template
<
typename
CT
,
size_t
S>
Tensor
(
const
CT
(&)[S]) -> Tensor<CT, S>;
int
main
() {
constexpr
char
encrypted_message[] =
"
Hello world!
"
;
constexpr
int
decryption_key[] = {-
5
, -
58
, -
65
, -
76
, -
6
,
83
, -
87
, -
2
, -
17
, -
5
,
5
,
66
,
0
};
constexpr
auto
tensor_1 =
Tensor
(encrypted_message);
constexpr
auto
tensor_2 =
Tensor
(decryption_key);
constexpr
auto
tensor_3 = tensor_1 + tensor_2;
static_assert
(tensor_3.
dump
() ==
"
C++ is magic
"
);
return
0
;
}
Back
|
FazBrowse Home
|
New Git URL