-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcopy_overload_solution.cpp
47 lines (40 loc) · 1.04 KB
/
copy_overload_solution.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
// Write a function template Copy that takes two pointers p and q and
// a std::size_t n, and copies one n adjacent elements starting at p
// into the array starting at q
//
// Overload Copy with a function that copies ints by using std::memcpy
//
// Overload Copy with a function template that copies all pointers
// using std::memcpy
//
// Verify that the “right” function is called automatically
#include <cstddef>
#include <cstring>
#include <iostream>
template <class T>
void Copy(T* p, T* q, std::size_t n)
{
std::cout << "Copy1" << std::endl;
while (n--)
*q++ = *p++;
}
void Copy(int* p, int* q, std::size_t n)
{
std::cout << "Copy2" << std::endl;
std::memcpy(q, p, n*sizeof(int));
}
template <class T>
void Copy(T** p, T** q, std::size_t n)
{
std::cout << "Copy3" << std::endl;
std::memcpy(q, p, n*sizeof(T*));
}
int main()
{
std::string x1[20] = {}, x2[20] = {};
int y1[20] = {}, y2[20] = {};
int* z1[20] = {}, *z2[20] = {};
Copy(x1, x2, 20);
Copy(y1, y2, 20);
Copy(z1, z2, 20);
}