我正在观看Jason Turner的一个视频,我看到你可以在函数范围内定义一个类型,并通过函数返回类型推导使其在该范围之外可用.
auto f()
{
struct MyStruct
{
int n;
};
return MyStruct{};
}
int main()
{
auto a = f().n;
return a;
}
Run Code Online (Sandbox Code Playgroud)
为什么允许这样做?C++ 14标准中是否有允许这样的段落?
当试图获得typeid的MyStruct与铛编译探险我在装配输出类型显示为看到f()::MyStruct,所以有一个范围,但不知何故,我可以访问MyStruct该范围之外.这是某种ADL的事吗?
我在 clang 上遇到编译错误,并使用以下代码对 gcc 发出警告:
static alignas(16) int one_s = 1; // clang: error: an attribute list cannot appear here; gcc: warning: attribute ignored;
static __attribute__((aligned(16))) int zero_s = 0; // on the other hand this works well on both compilers...
alignas(16) int one = 1; // this also works on both compilers
__attribute__((aligned(16))) int zero = 0; // as well as this
Run Code Online (Sandbox Code Playgroud)
有谁知道为什么在包含 static 关键字的声明中不接受alignas?我将 --std=c++11 编译器选项与 gcc 和 clang 一起使用。(编辑:我使用 clang 3.4 及更高版本和 gcc 4.8 及更高版本)
请注意,使用 Visual …