-
Notifications
You must be signed in to change notification settings - Fork 0
/
move_semantics.ixx
74 lines (61 loc) · 1.74 KB
/
move_semantics.ixx
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
66
67
68
69
70
71
72
73
74
module;
#include <iostream>
#include <memory>
#include <string>
#include <vector>
export module Playground.MoveSemantics;
class String
{
protected:
std::vector< char > m_data;
public:
// cppcheck-suppress noExplicitConstructor
String( const char *value ) : m_data( value, value + strlen( value ) )
{
std::cout << "Constructor String const char *(" << static_cast< void * >( this ) << ')' << std::endl;
}
String( const String &other ) : m_data( other.m_data.begin(), other.m_data.end() )
{
std::cout << "Construtor String (COPY)(" << static_cast< void * >( this ) << ')' << std::endl;
}
String( String &&other ) : m_data( std::move( other.m_data ) )
{
std::cout << "Construtor por String (MOVE)(" << static_cast< void * >( this ) << ')' << std::endl;
}
auto operator<=>( const String & ) const = default;
~String() { std::cout << "Destrutor String(" << static_cast< void * >( this ) << ')' << std::endl; }
void Print()
{
for( const auto &ch: m_data )
std::cout << ch;
std::cout << std::endl;
}
};
class Entity
{
protected:
String m_name;
public:
// cppcheck-suppress noExplicitConstructor
Entity( const String &name ) : m_name( name )
{
std::cout << "Construtor Entity (COPY)(" << static_cast< void * >( this ) << ')' << std::endl;
}
// cppcheck-suppress noExplicitConstructor
Entity( String &&name ) : m_name( std::move( name ) )
{
std::cout << "Construtor Entity (MOVE)(" << static_cast< void * >( this ) << ')' << std::endl;
}
~Entity() { std::cout << "Destrutor Entity(" << static_cast< void * >( this ) << ')' << std::endl; }
void PrintName() { m_name.Print(); }
};
export
{
void runMoveSemanticsTests()
{
const char *name = "Richard";
Entity e( name );
e.PrintName();
std::cout << "Fim." << std::endl;
}
}