为什么 msvc 编译器在显式调用析构函数时会发出未使用的变量

Why does msvc compiler issue unused variable when explicitly calling a destructor

使用以下代码:

struct Foo {};

template<class T>
void Destruct(T *obj)
{
    obj->~T();
}

int main(int /*argc*/, const char * /*argv*/[])
{
    char buffer[sizeof(Foo)];
    Destruct((Foo*)buffer);
    return 0;
}

Visual Studio 2015 将对未引用的参数发出警告:

warning C4100: 'obj': unreferenced formal parameter

这是合理的警告还是编译器中的错误?

在线重现:https://godbolt.org/z/xq96GU

编辑:将示例更新为完整示例

编辑 2:您需要启用 /W4 才能在 visual studio 2015 年发生,/W3 是不够的;也确认这不会发生在 2017 年。

编辑 3:对于 CNR,这里是命令行的输出,其中包含用于重现的所有参数:

>"C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin\CL.exe" /W4 test.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 19.00.24215.1 for x86
Copyright (C) Microsoft Corporation.  All rights reserved.

test.cpp
test.cpp(4): warning C4100: 'obj': unreferenced formal parameter
test.cpp(12): note: see reference to function template instantiation 'void Destruct<Foo>(T *)' being compiled
        with
        [
            T=Foo
        ]
Microsoft (R) Incremental Linker Version 14.00.24215.1
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:test.exe
test.obj

编辑 4 在 godbolt.org 上添加了示例复制 编辑5其实/W4重现就够了,/Wall没必要

恕我直言,这只是一种解释。

在专用函数void Destruct<Foo>(Foo *obj)中,传递的对象只用于调用一个空的析构函数。不涉及虚拟,没有变量变化值,没有发生IO。长话短说,该调用不会直接或间接观察到任何结果,因此编译器可以对其进行优化。因此,当编译器说传递的对象未被使用时,编译器是正确的在该特定专业化中。

此外,每个标准都需要很少的诊断,但据我所知,没有什么能阻止编译器对可疑代码发出警告。这正是这里发生的事情:编译器警告您,对于您的代码,对析构函数的调用将是空操作,因此代码是可疑的。但是没有编译错误,我假设程序 运行 没有问题,尽管强制转换违反了严格的别名规则。

所以我认为这个警告远非必需,不发出它的编译器是正确的,但由于代码奇怪,你不能责怪编译器亲切地警告你 程序员,我希望你知道你在这里做什么,因为我无法理解它背后的基本原理......