英文:
using declaration inside a member function for an inherited member field
问题
在函数内部,可以使用using
声明来将一个名称引入当前作用域,例如:
namespace A {
int y;
}
void f() { using A::y; }
using
声明可以在类定义中使用,以改变继承成员的可访问性,同时也可以显式地引入来自模板类的继承成员,例如:
template <bool activate>
struct A {
int x;
};
template <bool activate>
struct B : public A<activate> {
using A<activate>::x;
};
这样做特别有用,因为它避免了通过this->x
或A<activate>::x
来访问x
的需要。这只能在定义的类体内部使用,而不能在成员函数内部使用,例如:
template <bool activate>
struct A {
int x;
};
template <bool activate>
struct B : public A<activate> {
int f() const noexcept {
// 这将产生错误:"error: using-declaration for member at non-class scope"
// using A<activate>::x;
return x;
}
};
对于语言中这种限制的合理性,即using A<activate>::x
只能放在类的定义内部,是否有合理性的解释?
英文:
Inside a function one can employ the using declaration to import a name in the current scope, like
namespace A {
int y;
}
void f() { using A::y; }
A using declaration can be used in a class definition, to alter the accessibility of an inherited member, but also it is useful to explicitly bring a member inherited from a template class
template <bool activate>
struct A {
int x;
};
template <bool activate>
struct B : public A<activate> {
using A<activate>::x;
};
This is particularly useful, as it avoids the need to access to x
via this->x
or A<activate>::x
. This can be used only inside the body of the definition, but not inside a member function.
template <bool activate>
struct A {
int x;
};
template <bool activate>
struct B : public A<activate> {
int f() const noexcept {
// This gives: "error: using-declaration for member at non-class scope"
// using A<activate>::x;
return x;
}
};
Is there a rationale for this restriction of the language, that is, for the fact that using A<activate>::x
can only be placed inside the definition of the class?
答案1
得分: 2
在《C++设计与演化》中没有直接关于此主题的声明,因此很难可靠地推断出这样做的意图。 也就是说,直到最近为止,标准将using-declaration描述为对命名声明的同义词的引入声明。 从这个观点来看,将成员声明归属于块作用域会显得有些奇怪。 现在它们被视为在名称查找期间由其引用物取代的重定向,这与这种假设的用法更一致。
英文:
Absent a direct statement on the subject in Design & Evolution of C++, it’s hard to reliably infer intent for something like this. That said, until recently, the standard described using-declarations as introducing declarations as synonyms for the named declarations. In that view, it would be more than a little strange to have a member declaration belong to a block scope. Now they are considered to be redirects that are replaced by their referents during name lookup, which would be more consistent with this notional usage.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论