-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexplicit.cpp
More file actions
46 lines (34 loc) · 1.3 KB
/
explicit.cpp
File metadata and controls
46 lines (34 loc) · 1.3 KB
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
#pragma clang diagnostic ignored "-Wunused-variable"
#pragma clang diagnostic ignored "-Wunused-value"
#pragma clang diagnostic ignored "-Wunused-parameter"
#define CATCH_CONFIG_MAIN // Tells Catch2 to provide a main()
#include "../catch/catch_amalgamated.hpp"
#include "utils.h"
using namespace std;
namespace {
// explicit
// -
//
// Key notes:
// - explicit on constructor: blocks implicit conversions and copy-init
// - explicit on operator: blocks implicit conversion in assignments
// - C++20: explicit(bool) — conditionally explicit based on type trait
// - rule of thumb: mark single-arg constructors explicit unless conversion
// is intended
// - std::string is NOT explicit from const char* — intentional convenience
//// use case 1 — prevent implicit single-arg construction
struct Radius { double r; explicit Radius(double r) : r(r) {} };
void draw(Radius r) { };
TEST_CASE("explicit-1") {
// draw(5.0); // ❌ implicit conversion blocked
draw(Radius{5.0}); // ✓ explicit conversion
//// use case 2 — prevent implicit bool conversion
struct Handle {
explicit operator bool() const { return valid; }
bool valid = true;
};
Handle h;
if (h) {} // ✓ explicit bool context
// bool b = h; // ❌ implicit conversion blocked
}
}