forked from standardese/cppast
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpp_concept.cpp
More file actions
85 lines (69 loc) · 2.22 KB
/
Copy pathcpp_concept.cpp
File metadata and controls
85 lines (69 loc) · 2.22 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
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
75
76
77
78
79
80
81
82
83
84
85
// Copyright (C) 2017-2022 Jonathan Müller and cppast contributors
// SPDX-License-Identifier: MIT
#include <cppast/cpp_concept.hpp>
#include <cppast/cpp_function_template.hpp>
#include "test_parser.hpp"
using namespace cppast;
TEST_CASE("cpp_concept")
{
if (libclang_parser::libclang_minor_version() < 60)
return;
auto code = R"(
#include <concepts>
/// template<typename T>
/// concept a = requires(T t, int i)
/// {
/// {t.a()};
/// {t.b()} -> std::copy_constructible;
/// {t.c(i)} -> std::same_as<int>;
/// typename T::inner;
/// };
template<typename T>
concept a = requires(T t, int i)
{
{t.a()};
{t.b()} -> std::copy_constructible;
{t.c(i)} -> std::same_as<int>;
typename T::inner;
};
/// template<typename T>
/// concept b = a<T> && std::constructible_from<T, int>;
template<typename T>
concept b = a<T> && std::constructible_from<T, int>;
/// template<typename T>
/// void f1(T param);
template<typename T>
requires a<T>
void f1(T param);
/// template<b T>
/// void f2(T param);
template<b T>
void f2(T param);
/// template<std::convertible_to<int> T>
/// void f3(T param);
template<std::convertible_to<int> T>
void f3(T param);
)";
cpp_entity_index idx;
auto file = parse(idx, "cpp_concept.cpp", code, false, cppast::cpp_standard::cpp_20);
auto count = test_visit<cpp_concept>(*file, [&](const cpp_concept& con) {}, false);
REQUIRE(count == 2u);
count = test_visit<cpp_function_template>(*file, [&](const cpp_function_template& tfunc) {
REQUIRE(is_templated(tfunc.function()));
REQUIRE(!tfunc.scope_name());
check_template_parameters(tfunc, {{cpp_entity_kind::template_type_parameter_t, "T"}});
if(tfunc.name() == "f1")
{
REQUIRE(
static_cast<const cpp_template_type_parameter&>(*tfunc.parameters().begin()).keyword()
== cpp_template_keyword::keyword_typename);
}
else if (tfunc.name() == "f2" || tfunc.name() == "f3")
{
REQUIRE(static_cast<const cpp_template_type_parameter&>(*tfunc.parameters().begin())
.keyword()
== cpp_template_keyword::concept_contraint);
}
});
REQUIRE(count == 3u);
}