小编Dmi*_*riy的帖子

在指针解除引用时返回对本地临时对象的引用

Clang编译器生成警告编译此代码片段,我无法弄清楚原因.

const int* Get() {
    static const int ARRAY[4] = {1, 2, 3, 4};
    return &ARRAY[0];
}

const int& Test() {
    const auto& p = Get();
    return (*p);
}

warning: returning reference to local temporary object [-Wreturn-stack-address] 
    return (*p);
Run Code Online (Sandbox Code Playgroud)

GCC没有显示此代码的警告.我可以修复这样的片段:const auto p = Get(); 但我想知道是否有一些临时对象,问题更深层次

c++ return reference compiler-warnings clang++

7
推荐指数
1
解决办法
962
查看次数

标签 统计

c++ ×1

clang++ ×1

compiler-warnings ×1

reference ×1

return ×1