Clang Project

clang_source_code/test/SemaCXX/dllimport-constexpr.cpp
1// RUN: %clang_cc1 -std=c++14 %s -verify -fms-extensions -triple x86_64-windows-msvc
2// RUN: %clang_cc1 -std=c++17 %s -verify -fms-extensions -triple x86_64-windows-msvc
3
4__declspec(dllimport) void imported_func();
5__declspec(dllimport) int imported_int;
6struct Foo {
7  void __declspec(dllimport) imported_method();
8};
9
10// Instantiation is OK.
11template <void (*FP)()> struct TemplateFnPtr {
12  static void getit() { FP(); }
13};
14template <void (&FP)()> struct TemplateFnRef {
15  static void getit() { FP(); }
16};
17void instantiate1() {
18  TemplateFnPtr<&imported_func>::getit();
19  TemplateFnRef<imported_func>::getit();
20}
21
22// Check variable template instantiation.
23template <int *GI> struct TemplateIntPtr {
24  static int getit() { return *GI; }
25};
26template <int &GI> struct TemplateIntRef {
27  static int getit() { return GI; }
28};
29int instantiate2() {
30  int r = 0;
31  r += TemplateIntPtr<&imported_int>::getit();
32  r += TemplateIntRef<imported_int>::getit();
33  return r;
34}
35
36// Member pointer instantiation.
37template <void (Foo::*MP)()> struct TemplateMemPtr { };
38TemplateMemPtr<&Foo::imported_method> instantiate_mp;
39
40// constexpr initialization doesn't work for dllimport things.
41// expected-error@+1{{must be initialized by a constant expression}}
42constexpr void (*constexpr_import_func)() = &imported_func;
43// expected-error@+1{{must be initialized by a constant expression}}
44constexpr int *constexpr_import_int = &imported_int;
45// expected-error@+1{{must be initialized by a constant expression}}
46constexpr void (Foo::*constexpr_memptr)() = &Foo::imported_method;
47
48// We make dynamic initializers for 'const' globals, but not constexpr ones.
49void (*const const_import_func)() = &imported_func;
50int *const const_import_int = &imported_int;
51void (Foo::*const const_memptr)() = &Foo::imported_method;
52
53// Check that using a non-type template parameter for constexpr global
54// initialization is correctly diagnosed during template instantiation.
55template <void (*FP)()> struct StaticConstexpr {
56  // expected-error@+1{{must be initialized by a constant expression}}
57  static constexpr void (*g_fp)() = FP;
58};
59void instantiate3() {
60  // expected-note@+1 {{requested here}}
61  StaticConstexpr<imported_func>::g_fp();
62}
63