如何将类的静态方法引入到名称空间?
例如,我有一个类A
和一个静态方法foo
的类。我有一个命名空间nm
,并且想要将A :: foo引入到命名空间中。我试试以下如何将类的静态方法引入到名称空间?
namespace nm {
using A::foo;
void f()
{
foo(...); // use A::foo
}
}
但不能编译,因为A不是一个名称空间,因此使用指令在这里不起作用。任何方式来实现这个想法?我想在我的GUI项目中将它用于QObject :: tr和QObject :: connect以节省一些空间。
不直接。 [namespace.udecl]/8:
甲一类构件using声明应为成员声明。 [例:
struct X { int i; static int s; } void f() { using X::i; // error: X::i is a class member // and this is not a member declaration. using X::s; // error: X::s is a class member // and this is not a member declaration. }
- 结束例如]
但是你可以使用SFINAE和完美转发模拟foo
:
template <typename... Args>
auto foo(Args&&... args)
-> decltype(A::foo(std::forward<Args>(args)...))
{
return A::foo(std::forward<Args>(args)...);
}
嗯。也许我们应该使用'static static'来为类成员引入该名称的所有'static'符号。 – Deduplicator 2014-12-07 23:30:18
@Deduplicator这将永远不会被委员会接受。 – Columbo 2014-12-07 23:31:42
如果我没看错的,静态的方法是一个类方法,为什么它有可能使它成为一个名称空间函数?为什么不直接在你想要的任何东西中直接使用方法Class.method()? – nbro 2014-12-07 23:11:49