为什么不能将未命名结构用作尾随返回类型
Why can an unnamed struct not be used as a trailing return type?
struct { int a, b; } f(int x, int y) // OK
{
return { x, y };
}
auto g(int x, int y) -> struct { int a, b; } // error C2332
{
return { x, y };
}
int main()
{
auto n = f(1, 2).a; // OK
}
我的编译器是VC++ 2013 RC。
为什么g
错了,而f
没问题?
这是VC++的错误吗?
实际上,在C++中,在参数或返回类型中定义类型是非法的,无论是否命名。 请参阅 C++11[diff.decl]:
更改:在C++中,类型可能不在返回或参数类型中定义。在 C 中,允许这些类型定义
因此,实际问题是第一种情况被接受,而不是第二种情况被拒绝。
相关文章:
- 如何获取std::result_of函数的返回类型
- 奇怪的结构&GCC&clang(void*返回类型)
- 如何建立使用模板函数的lambda函数的尾部返回类型
- 为什么与常规GCC不同,即使有"学究性错误",MinGW-GCC也能容忍丢失的返回类型
- 在没有定义返回类型的函数中返回布尔值,并将结果保存在无错误的char编译中-为什么
- 特征::矩阵<双精度,1,3> 结构类型函数中的返回类型函数
- 函数作为模板参数,是否对返回类型强制约束
- C++中函数的向量返回类型引发错误
- 受保护的嵌套结构不能用作派生外部类中的返回类型?
- 为什么将函数的返回类型从结构节点*更改为void后,链表的元素没有显示create_ll和显示?
- 在Cython中定义C 结构返回类型
- 为什么在.h中定义的私有结构需要.cpp文件中的返回类型中的范围
- 从保存变量数据类型的数据结构中检索值,而不指定返回类型
- 作为返回类型的匿名结构
- 将结构指针声明为函数的返回类型
- 错误:"运算符="不匹配 |结构返回类型
- 私有结构(在类中定义)不能用作属于同一类的函数的返回类型吗
- 为什么不能将未命名结构用作尾随返回类型
- 尝试使用结构作为返回类型时出错
- 使用我的结构作为返回类型的问题