【发布时间】:2011-11-06 11:01:17
【问题描述】:
有没有办法在 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.
【问题讨论】:
-
新类型可能没有在返回类型中定义 - 好像编译器已经回答了你的问题......
标签: c++ anonymous-types anonymous-class