C++成员函数作用域中的using语句
如果我想使用模板派生类中模板基类的成员,我必须将其带入作用域,如下所示:
template <typename T>
struct base
{
void foo();
};
template <typename T>
struct derived : base<T>
{
using base<T>::foo;
};
为什么我不能将此 using 语句放入本地作用域,就像其他 using 语句一样?
template <typename T>
struct base
{
void foo();
};
template <typename T>
struct derived : base<T>
{
void f()
{
using base<T>::foo; // ERROR: base<T> is not a namespace
}
};
If I want to use a member of a template base class from a template derived class, I have to bring it into scope as such:
template <typename T>
struct base
{
void foo();
};
template <typename T>
struct derived : base<T>
{
using base<T>::foo;
};
Why can't I place this using statement into a local scope, like other using statements?
template <typename T>
struct base
{
void foo();
};
template <typename T>
struct derived : base<T>
{
void f()
{
using base<T>::foo; // ERROR: base<T> is not a namespace
}
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在函数作用域中
using base::foo
的目的是你想在函数中调用foo
,并且由于它给出错误,所以你不能这样做。如果你想调用该函数(否则你为什么要这样做),那么你可以执行这些允许的操作:
但是,你不能这样写:
ideone 上的演示: http://www.ideone.com/vfDNs
阅读本文以了解何时必须使用
函数调用中的 template
关键字:模板的丑陋编译器错误
The purpose of
using base<T>::foo
in the function scope is that you want to callfoo
in the function, and since it gives error, you cannot do that.If you want to call the functon (otherwise why you would do that), then you can do these which are allowed:
However, you cannot write this:
Demo at ideone : http://www.ideone.com/vfDNs
Read this to know when you must use
template
keyword in a function call:Ugly compiler errors with template
标准(草案 3225)在
[namespace.udecl]
中表示:但是,using-directive 没有这样的限制(
[namespace.udir]
):The standard (draft 3225) says in
[namespace.udecl]
:A using-directive has no such restriction, however (
[namespace.udir]
):