Reid Kleckner | 1a840d2 | 2018-05-10 18:57:35 +0000 | [diff] [blame] | 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; |
| 6 | struct Foo { |
| 7 | void __declspec(dllimport) imported_method(); |
| 8 | }; |
| 9 | |
| 10 | // Instantiation is OK. |
| 11 | template <void (*FP)()> struct TemplateFnPtr { |
| 12 | static void getit() { FP(); } |
| 13 | }; |
| 14 | template <void (&FP)()> struct TemplateFnRef { |
| 15 | static void getit() { FP(); } |
| 16 | }; |
| 17 | void instantiate1() { |
| 18 | TemplateFnPtr<&imported_func>::getit(); |
| 19 | TemplateFnRef<imported_func>::getit(); |
| 20 | } |
| 21 | |
| 22 | // Check variable template instantiation. |
| 23 | template <int *GI> struct TemplateIntPtr { |
| 24 | static int getit() { return *GI; } |
| 25 | }; |
| 26 | template <int &GI> struct TemplateIntRef { |
| 27 | static int getit() { return GI; } |
| 28 | }; |
| 29 | int 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. |
| 37 | template <void (Foo::*MP)()> struct TemplateMemPtr { }; |
| 38 | TemplateMemPtr<&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}} |
| 42 | constexpr void (*constexpr_import_func)() = &imported_func; |
| 43 | // expected-error@+1{{must be initialized by a constant expression}} |
| 44 | constexpr int *constexpr_import_int = &imported_int; |
| 45 | // expected-error@+1{{must be initialized by a constant expression}} |
| 46 | constexpr void (Foo::*constexpr_memptr)() = &Foo::imported_method; |
| 47 | |
| 48 | // We make dynamic initializers for 'const' globals, but not constexpr ones. |
| 49 | void (*const const_import_func)() = &imported_func; |
| 50 | int *const const_import_int = &imported_int; |
| 51 | void (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. |
| 55 | template <void (*FP)()> struct StaticConstexpr { |
| 56 | // expected-error@+1{{must be initialized by a constant expression}} |
| 57 | static constexpr void (*g_fp)() = FP; |
| 58 | }; |
| 59 | void instantiate3() { |
| 60 | // expected-note@+1 {{requested here}} |
| 61 | StaticConstexpr<imported_func>::g_fp(); |
| 62 | } |