c++ - POD的默认析构函数是静态的吗?

标签 c++ visual-c++ parameters warnings

我正在使用/Wall 进行编译,并收到警告 C4100: 'ptr' : unreferenced formal parameter warning 。该警告似乎是由调用 MSVC 的 std::_Container_proxy 上的析构函数引起的。 ,它有一个默认的析构函数。

我的代码:

template<class T>
class linear_allocator {
    //...other declarations...
    static void destroy(pointer ptr);
};
//...other definitions...
template<class T>
inline void linear_allocator<T>::destroy(typename linear_allocator<T>::pointer ptr)
{
    ptr->~T(); //line 262.  warning C4100: 'ptr' : unreferenced formal parameter
}
//ironically, this isn't a test case, this is my actual thingy class.  Go figure.
struct thingy { 
    unsigned int DATA;
    thingy() : DATA(0xABCDEF) {}
    ~thingy() {assert(DATA == 0xABCDEF);}
};
int main() {
    typedef std::vector<thingy, linear_allocator<thingy>> thingyholder;
    std::vector<thingyholder> holder;
}

警告全文为:

      f:\code\utilities\linear_allocator\linear_allocator.h(261): warning C4100: 'ptr' : unreferenced formal parameter
      f:\code\utilities\linear_allocator\linear_allocator.h(262) : while compiling class template member function 'void linear_allocator<T>::destroy(std::_Container_proxy *)'
      with
      [
          T=std::_Container_proxy
      ]
      f:\code\utilities\linear_allocator\linear_allocator.h(178) : while compiling class template member function 'linear_allocator<T>::~linear_allocator(void) throw()'
      with
      [
          T=std::_Container_proxy
      ]
      c:\program files\microsoft visual studio 10.0\vc\include\vector(454) : see reference to class template instantiation 'linear_allocator<T>' being compiled
      with
      [
          T=std::_Container_proxy
      ]
      c:\program files\microsoft visual studio 10.0\vc\include\vector(452) : while compiling class template member function 'std::_Vector_val<_Ty,_Alloc>::~_Vector_val(void)'
      with
      [
          _Ty=thingy,
          _Alloc=linear_allocator<thingy>
      ]
      c:\program files\microsoft visual studio 10.0\vc\include\vector(481) : see reference to class template instantiation 'std::_Vector_val<_Ty,_Alloc>' being compiled
      with
      [
          _Ty=thingy,
          _Alloc=linear_allocator<thingy>
      ]
      c:\program files\microsoft visual studio 10.0\vc\include\vector(1307) : see reference to class template instantiation 'std::vector<_Ty,_Ax>' being compiled
      with
      [
          _Ty=thingy,
          _Ax=linear_allocator<thingy>
      ]
      c:\program files\microsoft visual studio 10.0\vc\include\vector(1301) : while compiling class template member function 'void std::vector<_Ty>::_Tidy(void)'
      with
      [
          _Ty=thingyholder
      ]
      f:\code\utilities\linear_allocator\main.cpp(71) : see reference to class template instantiation 'std::vector<_Ty>' being compiled
      with
      [
          _Ty=thingyholder
      ]

我看到它正在使用 std::_Container_proxy 的析构函数,这很简单:

struct _Container_proxy
{   // store head of iterator chain and back pointer
_Container_proxy()
    : _Mycont(0), _Myfirstiter(0)
    {   // construct from pointers
    }

const _Container_base12 *_Mycont;
_Iterator_base12 *_Myfirstiter;
};

根据MSVC C4100: 'application' : unreferenced formal parameter warning ,这可能会发生 if The functions you are calling using the application object are static functions, so they aren't really referencing the application object.std::_Container_proxy看起来是一个 POD,这是否意味着默认析构函数是静态的作为优化?

(是的,我知道各种解决方法可以使警告消失。我想在输入 ptr=ptr; //warning workaround 之前确定为什么会收到警告。)

最佳答案

这是 Visual C++ 中的一个已知错误:"Visual C++ gives unexpected warning C4100 on explicit call to object destructor" .

可以安全地忽略该警告(或通过#pragma warning/Wd4100抑制)。

关于c++ - POD的默认析构函数是静态的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7247153/

相关文章:

c++ - 重载 operator== 提示 'must take exactly one argument'

python - 需要哪个 $path 以便 g++/pybind11 可以找到 Python.h?

c# - 通过连接字符串或连接对象设置 MySQL 变量?

java - Java中的惰性参数类型?

c++ - 无法使用自定义类创建 QList

c++ - CPU使用率高的常见原因是什么?

c++ - 创建一个可以运行的DLL

c++ - MSVC 如何优化静态变量的使用?

multithreading - VC++ 2010 Express 没有 std::thread 吗?

delphi - 将 Delphi 字符串作为参数发送给 DLL