小编blu*_*eck的帖子

为什么我可以通过返回类型推导从函数外部访问函数内定义的结构?

我正在观看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标准中是否有允许这样的段落?

当试图获得typeidMyStruct与铛编译探险我在装配输出类型显示为看到f()::MyStruct,所以有一个范围,但不知何故,我可以访问MyStruct该范围之外.这是某种ADL的事吗?

c++ c++14

12
推荐指数
1
解决办法
352
查看次数

为什么在使用 clang 进行静态声明时,alignas 无法编译?

我在 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 …

c++ static clang language-lawyer alignas

5
推荐指数
1
解决办法
1304
查看次数

标签 统计

c++ ×2

alignas ×1

c++14 ×1

clang ×1

language-lawyer ×1

static ×1