gpt4 book ai didi

c++ - 并非针对所有未使用的静态常量变量都引发未使用变量警告

转载 作者:行者123 更新时间:2023-11-30 05:10:53 26 4
gpt4 key购买 nike

您能帮我理解为什么我不是针对所有未使用的静态常量变量都收到未使用变量警告吗?我准备了一个简单的代码。这里有 2 个文件 main.cpp 和 incl.h。

包括.h

#ifndef INCL_H
#define INCL_H

struct A
{
static A& instance()
{
static A a;
return a;
}
};

static const A a = A::instance();
static const A& b = A::instance();

#endif

main.cpp

#include "incl.h"

int main ()
{
return 0;
}

我预计会有 2 个未使用变量警告(针对 a 和 b fariables),但实际上警告只有一个针对变量 b

$ g++ -std=c++11 -Wall -Wextra -O2 -c main.cpp
In file included from main.cpp:1:0:
incl.h:14:17: warning: ‘b’ defined but not used [-Wunused-variable]
static const A& b = A::instance();

为什么变量 a 不会引起同样的警告?

最佳答案

对于变量 a,单例实例通过赋值被复制到变量中,因此您可以争辩说它正在被使用。

关于c++ - 并非针对所有未使用的静态常量变量都引发未使用变量警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45420957/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com