File: dllimport-constexpr.cpp

package info (click to toggle)
llvm-toolchain-19 1%3A19.1.7-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,998,520 kB
  • sloc: cpp: 6,951,680; ansic: 1,486,157; asm: 913,598; python: 232,024; f90: 80,126; objc: 75,281; lisp: 37,276; pascal: 16,990; sh: 10,009; ml: 5,058; perl: 4,724; awk: 3,523; makefile: 3,167; javascript: 2,504; xml: 892; fortran: 664; cs: 573
file content (62 lines) | stat: -rw-r--r-- 2,207 bytes parent folder | download | duplicates (27)
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();
}