dllimport-constexpr.cpp
2.16 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
// RUN: %clang_cc1 -std=c++14 %s -verify -fms-extensions -triple x86_64-windows-msvc
// RUN: %clang_cc1 -std=c++17 %s -verify -fms-extensions -triple x86_64-windows-msvc
__declspec(dllimport) void imported_func();
__declspec(dllimport) int imported_int;
struct Foo {
void __declspec(dllimport) imported_method();
};
// Instantiation is OK.
template <void (*FP)()> struct TemplateFnPtr {
static void getit() { FP(); }
};
template <void (&FP)()> struct TemplateFnRef {
static void getit() { FP(); }
};
void instantiate1() {
TemplateFnPtr<&imported_func>::getit();
TemplateFnRef<imported_func>::getit();
}
// Check variable template instantiation.
template <int *GI> struct TemplateIntPtr {
static int getit() { return *GI; }
};
template <int &GI> struct TemplateIntRef {
static int getit() { return GI; }
};
int instantiate2() {
int r = 0;
r += TemplateIntPtr<&imported_int>::getit();
r += TemplateIntRef<imported_int>::getit();
return r;
}
// Member pointer instantiation.
template <void (Foo::*MP)()> struct TemplateMemPtr { };
TemplateMemPtr<&Foo::imported_method> instantiate_mp;
// constexpr initialization doesn't work for dllimport things.
// expected-error@+1{{must be initialized by a constant expression}}
constexpr void (*constexpr_import_func)() = &imported_func;
// expected-error@+1{{must be initialized by a constant expression}}
constexpr int *constexpr_import_int = &imported_int;
// expected-error@+1{{must be initialized by a constant expression}}
constexpr void (Foo::*constexpr_memptr)() = &Foo::imported_method;
// We make dynamic initializers for 'const' globals, but not constexpr ones.
void (*const const_import_func)() = &imported_func;
int *const const_import_int = &imported_int;
void (Foo::*const const_memptr)() = &Foo::imported_method;
// Check that using a non-type template parameter for constexpr global
// initialization is correctly diagnosed during template instantiation.
template <void (*FP)()> struct StaticConstexpr {
// expected-error@+1{{must be initialized by a constant expression}}
static constexpr void (*g_fp)() = FP;
};
void instantiate3() {
// expected-note@+1 {{requested here}}
StaticConstexpr<imported_func>::g_fp();
}