Guest User

Bug

a guest
Feb 24th, 2016
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.70 KB | None | 0 0
  1. // This example illustrates a bug in GCC, Clang and VC++,
  2. // which allows a pointer to function type with cv-qualifiers to be formed and used.
  3. #include <iostream>
  4.  
  5. template<typename X>
  6. struct get_pointer
  7. {
  8. private:
  9.     template<typename F>
  10.     struct extract;
  11.  
  12.     template<typename F>
  13.     struct extract<int(F)>
  14.     {
  15.         typedef F ptr_t;
  16.     };
  17. public:
  18.  
  19.     // Function type X in an argument should become a pointer (8.3.5/5).
  20.     // Since we later use function type that has volatile qualifier,
  21.     // a pointer to such type is being formed here - which is prohibited,
  22.     // see 8.3.1 note 4. This program is ill-formed.
  23.     // Out of GCC, Clang and VC++ only VC++ generates an
  24.     // error - but only if the argument doesn't have a name (quite strange)
  25.     // (i.e
  26.     //      typedef int func_t(X value);
  27.     // will actually generate an error in VC++).
  28.     typedef int func_t(X);
  29.  
  30.     typedef typename extract<func_t>::ptr_t type;
  31. };
  32.  
  33. void f() { std::cout << "x" << std::endl; }
  34.  
  35. // volatile in function typedef is allowed.
  36. typedef void volatile_function_t() volatile;
  37.  
  38. // this is the line that should cause an error but does not:
  39. typedef get_pointer<volatile_function_t>::type volatile_function_ptr_t;
  40.  
  41. int main()
  42. {
  43.  
  44. #ifdef __clang__ /* for Clang at least a cast is required */
  45.  
  46.     volatile_function_ptr_t z = (volatile_function_ptr_t)(&f);
  47.  
  48. #else /* VC++ and GCC happily accept it even without a cast */
  49.  
  50.     volatile_function_ptr_t z = &f;
  51.  
  52. #endif
  53.  
  54.     z(); // Call our volatile non-member function.
  55.          // When compiled with Clang or VC++ a usual call to f() is generated.
  56.          // However GCC generates incorrect code - the resulting program segfaults.
  57. }
Advertisement
Add Comment
Please, Sign In to add comment