xref: /llvm-project/clang/test/SemaCXX/function-type-qual.cpp (revision 82a32140acb52472241f04644cdcf88a4cf4bee8)
1 // RUN: %clang_cc1 -fsyntax-only -verify %s
2 
3 void f() const; // expected-error {{non-member function cannot have 'const' qualifier}}
4 void (*pf)() const; // expected-error {{pointer to function type cannot have 'const' qualifier}}
5 extern void (&rf)() const; // expected-error {{reference to function type cannot have 'const' qualifier}}
6 
7 typedef void cfn() const;
8 cfn f2; // expected-error {{non-member function of type 'cfn' (aka 'void () const') cannot have 'const' qualifier}}
9 
10 class C {
11   void f() const;
12   cfn f2;
13   static void f3() const; // expected-error {{static member function cannot have 'const' qualifier}}
14   static cfn f4; // expected-error {{static member function of type 'cfn' (aka 'void () const') cannot have 'const' qualifier}}
15 
m1()16   void m1() {
17     x = 0;
18   }
19 
m2() const20   void m2() const { // expected-note {{member function 'C::m2' is declared const here}}
21     x = 0; // expected-error {{cannot assign to non-static data member within const member function 'm2'}}
22   }
23 
24   int x;
25 };
26 
27 void (C::*mpf)() const;
28 cfn C::*mpg;
29 
30 // Don't crash!
31 void (PR14171)() const; // expected-error {{non-member function cannot have 'const' qualifier}}
32 
33 // Test template instantiation of decayed array types.  Not really related to
34 // type quals.
arrayDecay(const T a[])35 template <typename T> void arrayDecay(const T a[]) { }
instantiateArrayDecay()36 void instantiateArrayDecay() {
37   int a[1];
38   arrayDecay(a);
39 }
40 
41 namespace GH79748 {
42 typedef decltype(sizeof(0)) size_t;
43 struct A {
44   void* operator new(size_t bytes) const; //expected-error {{static member function cannot have 'const' qualifier}}
45   void* operator new[](size_t bytes) const; //expected-error {{static member function cannot have 'const' qualifier}}
46 
47   void operator delete(void*) const; //expected-error {{static member function cannot have 'const' qualifier}}
48   void operator delete[](void*) const; //expected-error {{static member function cannot have 'const' qualifier}}
49 };
50 struct B {
51   void* operator new(size_t bytes) volatile; //expected-error {{static member function cannot have 'volatile' qualifier}}
52   void* operator new[](size_t bytes) volatile; //expected-error {{static member function cannot have 'volatile' qualifier}}
53 
54   void operator delete(void*) volatile; //expected-error {{static member function cannot have 'volatile' qualifier}}
55   void operator delete[](void*) volatile; //expected-error {{static member function cannot have 'volatile' qualifier}}
56 };
57 }
58