Re: Ограницения template type arguments
От: tolgamrab Россия  
Дата: 05.02.10 17:17
Оценка:
Здравствуйте, Аноним, Вы писали:

А>Большинство компайлеров транслируют:

А>
А>template<class T> struct A {
А>static T t; // 1
А>T *pt;      // 2
А>};

А>typedef int function();
А>A<function> a;  // ill-formed: would declare A<function>::t
А>                // as a static member function
А>

А>Comeau ругает строку с // 1
А>

14.3.1/3 If a declaration acquires a function type through a type dependent on a template-parameter and this causes a declaration that does not use the syntactic form of a function declarator to have function type, the program is ill-formed.


А>Проясните ситуацию в случае 2, должен ли компайлер разрешать, или нет? какие причины запрета в 1?


Как раз на днях активно исследовал тему функционального типа, так как еще месяц назад о таком не подозревал.
Дьюхарст пишет по этому поводу, что при помощи typedef'а можно объявлять только нестатические методы класса. Однако в MVS 2008 и в MVS 6.0 прекрасно компилируются и статические методы классов и глобальные функции определенные через typedef.
Описание таких определений приводится в

8.3.5/7 A typedef of function type may be used to declare a function but shall not be used to define a function (dcl.fct.def). [Example:

typedef void F();
F fv; // OK: equivalent to void fv();
F fv { } // ill-formed
void fv() { } // OK: definition of fv


--- end example] A typedef of a function type whose declarator includes a cv-qualifier-seq shall be used only to declare the function type for a nonstatic member function, to declare the function type to which a pointer to member refers, or to declare the top-level function type of another function typedef declaration. [Example:

typedef int FIC(int) const;
FIC f; // ill-formed: does not declare a member function
struct S {
FIC f; // OK
};
FIC S::*pm = &S::f; // OK


--- end example]


Однако 14.3.1/3 похоже запрещает такую же конструкцию для шаблонов. Печально, я только придумал как это можно красиво использовать...
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.