-
Notifications
You must be signed in to change notification settings - Fork 1
/
cppFriends20.cpp
60 lines (48 loc) · 1.14 KB
/
cppFriends20.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
49
50
51
52
53
54
55
56
57
58
59
60
// C++20 or later required
#include <sstream>
#include <gtest/gtest.h>
#if __cplusplus >= 202002L
template <class T>
concept Printable = requires (const T& x) {
x.to_string();
};
template <Printable T>
void printObject(std::ostream& os, const T& x) {
os << x.to_string();
}
template <typename T>
void printObject(std::ostream& os, const T& x) {
os << "?";
}
namespace {
class PrintableClass {
public:
PrintableClass(const std::string& name) : name_(name) {
}
const std::string to_string(void) const {
return name_;
};
private:
std::string name_;
};
struct NonPrintableClass {};
}
class TestConcept : public ::testing::Test {};
TEST_F(TestConcept, PrintableConcept) {
std::ostringstream os;
PrintableClass aPrintable("Foo");
printObject(os, aPrintable);
EXPECT_EQ("Foo", os.str());
NonPrintableClass aNonPrintable;
printObject(os, aNonPrintable);
EXPECT_EQ("Foo?", os.str());
}
#endif
/*
Local Variables:
mode: c++
coding: utf-8-dos
tab-width: nil
c-file-style: "stroustrup"
End:
*/