Использование объявления, которое ссылается на шаблоны членов базового класса. - PullRequest
4 голосов
/ 16 августа 2010

Может ли кто-нибудь объяснить это

 "A using-declaration in a derived class cannot refer to a specialization 
 of a template conversion function in a base class."

это из стандарта ISO C ++ ..14.5.2, точка 7

Ответы [ 2 ]

5 голосов
/ 16 августа 2010

Это означает, что это неправильно:

struct A { template<typename T> operator T(); };
struct B : A { using A::operator int; }; // ill-formed: refers to specialization

Аналогично для других специализаций шаблонов функций (не только функций преобразования)

struct A { template<typename T> void f(); };
struct B : A { using A::f<int>; }; // ill-formed: refers to specialization
0 голосов
/ 16 августа 2010
2.)

Q: My compiler says that a member of a base class template is not defined in a derived class template. Why is it not inherited?

template<typename T>
class base {
public:
    void base_func();
};

template<typename T>
class derived : public base<T> {
public:
    void derived_func()
    {
        base_func(); // error: base_func not defined
    }
};

A: 
It is inherited. 
However, the standard says that unqualified names in a template are generally non-dependent and must be looked up when the template is defined. 
Since the definition of a dependent base class is not known at that time (there may be specialisations of the base class template that have not yet been seen), 
unqualified names are never resolved to members of the dependent base class. 
Where names in the template are supposed to refer to base class members or to indirect base classes, 
they can either be made dependent by qualifying them or brought into the template's scope with a using-declaration. 
In the example, this could be achieved by replacing the call to base_func() with this->base_func() or base<T>::base_func(), or by adding the declaration using base<T>::base_func;.

то, что я получаю с этой страницы, это то, что для того, чтобы ввести в класс функцию из родительского шаблонного класса, вы должны использовать using-Декларация, которая выглядит как base<T>::base_func() и что стандарт ISO говорит о том, что вы не можете использовать объявление использования, подобное этому base<int>::base_func()Разница между <>.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...