Eon*_*nil 6 c++ static-methods decltype c++11
当我声明一个类静态方法时,是否可以使用decltype(或任何其他类似的样式)引用当前类?例如,
class
AAA
{
static AAA const make();
};
Run Code Online (Sandbox Code Playgroud)
我想做这样的事情.
class
AAA
{
static decltype(*this) const make(); // Not working because there's no `this`.
};
Run Code Online (Sandbox Code Playgroud)
本*this是用来描述我想做的事情.我想知道一些decltype()可以解决的表达方式AAA.
如果有可能我该怎么办?
在C++ 1y中你可以这样做:
class
AAA
{
public:
static auto make()
{
return AAA();
}
};
int main()
{
AAA aaa = AAA::make();
}
Run Code Online (Sandbox Code Playgroud)
这在C++ 11中是不合法的,因为您需要为其指定返回类型make().在C++ 98/03/11中,您可以:
class
AAA
{
public:
typedef AAA Self;
static Self make()
{
return AAA();
}
};
Run Code Online (Sandbox Code Playgroud)
这是低技术,但非常可读.
<aside>
您应该避免按值返回const限定类型.这抑制了有效的移动语义.如果要避免分配给rvalues,则创建一个使用的限定赋值运算符&.
</aside>
| 归档时间: |
|
| 查看次数: |
1285 次 |
| 最近记录: |