decltype(auto)
语法一起使用括号。但这不是当前的用例。)示例代码:
#include <iostream>
#include <cstdio>
struct Foo
{
Foo() { std::cout << "default constructor" << std::endl; }
Foo(const Foo& rhs) { std::cout << "copy constructor" << std::endl; }
Foo(Foo&& rhs) { std::cout << "move constructor" << std::endl; }
Foo& operator=(const Foo& rhs) { std::cout << "copy assignment" << std::endl; return *this; }
Foo& operator=(Foo&& rhs) { std::cout << "move assignment" << std::endl; return *this; }
};
Foo foo_normal()
{
Foo a{};
return a;
}
Foo foo_parentheses()
{
Foo a{};
return (a);
}
int main()
{
auto a = foo_normal();
auto b = foo_parentheses();
std::getchar();
}
在线编译器1: http://cpp.sh/75bux
在线编译器2: http://coliru.stacked-crooked.com/a/c266852b9e1712f3
在Visual Studio中以发布模式输出的结果为:
default constructor
default constructor
在另外两个编译器中,输出结果为:
default constructor
default constructor
move constructor
(a)
不是object
的名称。也许将其标记为[tag:language-lawyer]。 - nwp