有没有办法在C ++中使用匿名类作为返回类型?
我用谷歌搜索这可行:
struct Test {} * fun()
{
}
但是这段代码没有编译,错误信息是:
可能无法在返回类型中定义新类型
实际上代码没有任何意义,我只想弄清楚一个匿名类是否可以在C ++中用作返回类型。
这是我的代码:
#include <typeinfo>
#include <iterator>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdlib>
using namespace std;
int main(int argc, char **argv)
{
int mx = [] () -> struct { int x, y ; } { return { 99, 101 } ; } ().x ;
return 0;
}
我使用g ++ xx.cpp -std = c ++ 0x编译此代码,编译器编译:
expected primary-expression before '[' token.
注意:这些代码片段不再适用于最新版本的g ++。我用版本4.5.2编译它们,但版本4.6.1和4.7.0不再接受它们。
您 能够 在C ++ 11中声明一个匿名结构作为lambda函数的返回类型。但它并不漂亮。此代码将值99分配给 mx
:
int mx = [] () -> struct { int x, y ; } { return { 99, 101 } ; } ().x ;
ideone输出在这里: http://ideone.com/2rbfM
响应cheng的要求:
lambda函数是C ++ 11中的一个新特性。它基本上是一个匿名函数。这是一个简单的lambda函数示例,它不带参数并返回一个 int
:
[] () -> int { return 99 ; }
您可以将其分配给变量(您必须使用 auto
去做这个):
auto f = [] () -> int { return 99 ; } ;
现在你可以像这样调用它:
int mx = f() ;
或者你可以直接调用它(这是我的代码所做的):
int mx = [] () -> int { return 99 ; } () ;
我的代码只是使用 struct { int x, y ; }
代替 int
。该 .x
最后是正常的 struct
成员语法应用于函数的返回值。
此功能并不像可能出现的那样无用。您可以多次调用该函数,以访问不同的成员:
auto f = [] () -> struct {int x, y ; } { return { 99, 101 } ; } ;
cout << f().x << endl ;
cout << f().y << endl ;
您甚至不必两次调用该函数。此代码完全符合OP的要求:
auto f = [] () -> struct {int x, y ; } { return { 99, 101 } ; } () ;
cout << f.x << endl ;
cout << f.y << endl ;
不是他们不能。如错误消息所示,来自ISO / IEC 14882:2011 8.3.5 / 9:
不应在返回或参数类型中定义类型。函数定义的参数类型或返回类型不应是不完整的类类型(可能是cv限定的),除非函数定义嵌套在该类的成员规范中(包括在类中定义的嵌套类中的定义) )。
当然,您不能将现有的匿名类型命名为函数声明中的返回类型,因为匿名类没有名称。
虽然你可以创建一个 typedef
对于一个未命名的类并将其用作返回类型,因为typedef名称成为用于链接目的的类类型的名称,该类实际上不再是匿名的。
struct Test {} * a;
decltype(a) fun() {
return a;
}
顺便说一句, struct Test {}
不是一个匿名结构。
不,你不能在C ++中做那样的匿名类型。
但是,您可以使用 typedef
分配匿名类型新名称。
typedef struct
{
unsigned x;
unsigned y;
} TPoint;
正如@ Charles的帖子几乎回答了直接引用规范的问题。
现在,我想为什么 匿名 type不能是函数的返回类型,是因为假设 f
返回一个匿名类型,然后在调用站点写什么?
????? obj = f();
应该写什么来代替 ?????
在上面的代码?
在C ++ 14中,最接近你想要的是这个:
auto f() {
struct {
int x, y;
} ret{10,24};
return ret;
}
int main() {
printf("%i", f().x);
}
结构是匿名的(ret是变量名,而不是类型名),并返回。
如果需要,你仍然可以得到它
using my_struct = decltype(f());
my_struct another; another.x++;