[C++0x] decltype от this в static member function
От: wander  
Дата: 08.12.11 06:14
Оценка:
Читаю новый стандарт, возник сабжевый вопрос теоретического характера.
В 9.4.1 написано "a static member function does not have a this pointer". Это понятно, но уточнений насчет decltype и подобного нет. Кто в теме, просветите.

struct A
{
   // Это работает в gcc 4.7 (сборка из снапшота от 3 числа сего месяца)
   static void foo1()
   {
      typedef decltype(this) this_pointer;
   }
   // Это тоже работает 
   static auto foo2() -> decltype(this)
   {
      return nullptr;
   }
};
avalon 1.0rc3 build 426, zlib 1.2.3
Re: [C++0x] decltype от this в static member function
От: Masterkent  
Дата: 08.12.11 10:37
Оценка: 6 (1)
wander:

W>Читаю новый стандарт, возник сабжевый вопрос теоретического характера.

W>В 9.4.1 написано "a static member function does not have a this pointer". Это понятно, но уточнений насчет decltype и подобного нет. Кто в теме, просветите.

W>
W>struct A
W>{
W>   // Это работает в gcc 4.7 (сборка из снапшота от 3 числа сего месяца)
W>   static void foo1()
W>   {
W>      typedef decltype(this) this_pointer;
W>   }
W>   // Это тоже работает 
W>   static auto foo2() -> decltype(this)
W>   {
W>      return nullptr;
W>   }
W>};
W>

На данный случай распространяются правила 5.1.1/3:

If a declaration declares a member function or member function template of a class X, the expression this is a prvalue of type “pointer to cv-qualifier-seq X” between the optional cv-qualifer-seq and the end of the function-definition, member-declarator, or declarator. It shall not appear before the optional cv-qualifier-seq and it shall not appear within the declaration of a static member function (although its type and value category are defined within a static member function as they are within a non-static member function). [ Note: this is because declaration matching does not occur until the complete declarator is known. —end note ] Unlike the object expression in other contexts, *this is not required to be of complete type for purposes of class member access (5.2.5) outside the member function body. [ Note: only class members declared prior to the declaration are visible. —end note ] [ Example:

struct A {
  char g();
  template<class T> auto f(T t) -> decltype(t + g())
    { return t + g(); }
};
template auto A::f(int t) -> decltype(t + g());

—end example ]

 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.