-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patha.cpp
48 lines (40 loc) · 1.19 KB
/
a.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
#include <iostream>
#include <string_view>
#include <cstdlib>
#include <stdexcept>
namespace app {
class Greeter {
public:
// Use string_view for efficient string passing
explicit Greeter(std::string_view message) noexcept
: m_message(message) {}
// Mark member functions that don't modify state as const
[[nodiscard]] auto getMessage() const noexcept -> std::string_view {
return m_message;
}
// Use auto return type for better maintainability
auto greet() const -> void {
std::cout << getMessage() << '\n';
}
private:
std::string_view m_message;
};
// Use [[nodiscard]] to ensure return values are handled
[[nodiscard]] auto createGreeter(std::string_view message) -> Greeter {
if (message.empty()) {
throw std::invalid_argument("Message cannot be empty");
}
return Greeter{message};
}
} // namespace app
auto main() -> int {
try {
constexpr std::string_view message = "Hello, World!";
auto greeter = app::createGreeter(message);
greeter.greet();
return EXIT_SUCCESS;
} catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << '\n';
return EXIT_FAILURE;
}
}