首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用引用基类的成员模板的声明。

使用引用基类的成员模板的声明。
EN

Stack Overflow用户
提问于 2010-08-16 21:01:56
回答 2查看 3.5K关注 0票数 4

有人能解释一下吗?

代码语言:javascript
复制
 "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点

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-08-16 21:36:57

这意味着这是病态的:

代码语言:javascript
复制
struct A { template<typename T> operator T(); };
struct B : A { using A::operator int; }; // ill-formed: refers to specialization

同样,对于其他函数模板专门化(不仅仅是转换函数)

代码语言:javascript
复制
struct A { template<typename T> void f(); };
struct B : A { using A::f<int>; }; // ill-formed: refers to specialization
票数 5
EN

Stack Overflow用户

发布于 2010-08-16 21:30:51

代码语言:javascript
复制
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;.

  • http://womble.decadent.org.uk/c++/template-faq.html

我从这个页面得到的是,要将来自父模板类的函数引入到类中,你必须使用using -声明,它看起来像base<T>::base_func(),ISO标准规定你不能使用像这个base<int>::base_func()这样的using声明。不同之处在于<>。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3493332

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档