Skip to content

Commit 692f416

Browse files
authored
Merge pull request #40 from nickrolfe/dependent_template_alias
C++: dependent template alias
2 parents fdfbfb3 + df1f514 commit 692f416

File tree

5 files changed

+63
-6
lines changed

5 files changed

+63
-6
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2+
template <typename>
3+
using Z = int;
4+
5+
template <typename T, typename U = int>
6+
struct Thing {
7+
int x;
8+
};
9+
10+
template <typename T>
11+
struct Thing<T, Z<typename T::Undefined>> {
12+
int y;
13+
};
14+
15+
// Note that float::Undefined is an error, so this should match the primary
16+
// template, not the partial specialization.
17+
Thing<float> thing_float;
18+
19+
void f() {
20+
// If we incorrectly matched the partial specialization, this write to x would
21+
// be an error.
22+
thing_float.x = 1;
23+
}
24+
25+
// Now, a type that actually does define Undefined
26+
struct S {
27+
using Undefined = int;
28+
};
29+
30+
// S::Undefined is okay, so this should match the partial specialization.
31+
Thing<S> thing_s;
32+
33+
void g() {
34+
// If we incorrectly matched the primary template, this write to y would be an
35+
// error.
36+
thing_s.y = 1;
37+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
| test.cpp:17:14:17:24 | thing_float | test.cpp:6:8:6:12 | Thing<float, int> | test.cpp:7:7:7:7 | x |
2+
| test.cpp:31:10:31:16 | thing_s | test.cpp:11:8:11:41 | Thing<S, int> | test.cpp:12:7:12:7 | y |
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import cpp
2+
3+
from Variable v, Class c
4+
where c = v.getType()
5+
and v.getFile().getBaseName() = "test.cpp"
6+
select v, c, c.getAMemberVariable()

cpp/ql/test/library-tests/templates/typedefs/template_typedefs.cpp

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ void functions() {
1212
b(static_cast<my_int>(0));
1313
c<int>(0);
1414
d<my_int>(0);
15-
15+
1616
e<int>(0);
1717
e<my_int>(0);
18-
18+
1919
f<my_int>(0);
2020
f<int>(0);
2121
}
@@ -27,25 +27,35 @@ template <typename TD> struct D {};
2727
template <typename TE> struct E {};
2828
template <typename TF> struct F {};
2929
template <typename TG> struct G {};
30+
template <typename TH> struct H {};
3031

3132
struct S { int x; };
3233
typedef S S_t;
3334

3435
typedef C<int> C1;
3536
typedef D<my_int> D1;
3637

38+
template <typename TZ>
39+
using Z = TZ;
40+
3741
void types() {
3842
A<int>* a;
3943
B<my_int>* b;
4044
C1 c;
4145
D1 d;
42-
46+
4347
E<int> e1;
4448
E<my_int> e2;
45-
49+
E<Z<int>> e3;
50+
4651
F<my_int> f1;
4752
F<int> f2;
48-
53+
F<Z<int>> f3;
54+
55+
H<Z<int>> h1;
56+
H<my_int> h2;
57+
H<int> h3;
58+
4959
G<my_int*> g1;
5060
G<const my_int* const> g2;
5161
G<my_int**&> g3;

cpp/ql/test/library-tests/templates/typedefs/template_typedefs.expected

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@
1212
| F<int> | int |
1313
| G<..(*)(..)> | pointer to {function returning {pointer to {int}} with arguments (int)} |
1414
| G<TG> | TG |
15-
| G<__attribute((vector_size(32))) int> | {GNU 8 element vector of {int}} |
15+
| G<__attribute((vector_size(32))) int> | GNU 8 element vector of {int} |
1616
| G<const int *const> | const {pointer to {const {int}}} |
1717
| G<int **&> | reference to {pointer to {pointer to {int}}} |
1818
| G<int *> | pointer to {int} |
1919
| G<int S::*> | pointer to member of S with type {int} |
2020
| G<int(&)[3]> | reference to {array of 3 {int}} |
21+
| H<TH> | TH |
22+
| H<int> | int |
2123
| a | Ta |
2224
| a | int |
2325
| b | Tb |

0 commit comments

Comments
 (0)