clang
和 Visual Studio 编译器(但不是gcc
)允许编写如下代码:
struct A
{
operator auto() { return 0; }
};
int main()
{
A a;
a.operator auto();
}
你能解释一下是operator auto
什么吗?它是特定编译器的扩展还是标准语言功能,如果是,它是在什么语言标准(例如 C++17)中出现的?
当auto
在用户定义的转换函数中使用时,类型将通过返回类型推导来推导,即int
对于这种情况 ( 0
)。这是从 C++14 开始引入的。
占位符 auto 可用于convert-type-id,表示推导的返回类型:
struct X { operator int(); // OK operator auto() -> short; // error: trailing return type not part of syntax operator auto() const { return 10; } // OK: deduced return type };
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句