模板好友操作员新明W32

template friend operator new mingw32

本文关键字:新明 W32 操作员 好友      更新时间:2023-10-16

我的理解是运算符new无法模板化(当使用我使用的参数声明时)。(size_t)

以下编译在mingw32和arduino DUE上可以,但在其他平台(例如Linux)上则不行。

mingw32 中是否存在错误,或者是否也应该在其他目标上允许这样做?

class t_other_class
{
  public:
  // This form of operator new is ***NOT*** allowed to be templated.
  void*                  operator new     ( size_t            arg1  ) ;
  // This form of operator new CAN be templated, and IS.
  template<typename TF>
  void*                  operator new     ( size_t            arg1
                                          , int               arg2
                                          ) ;
  // This form of operator new CAN be templated, but IS NOT.
  void*                  operator new     ( size_t            arg1
                                          , int               arg2
                                          , int               arg3
                                          ) ;
};

template <typename TCLASS>
class t_class
{
  public:
    // This form of operator new can NOT be templated, but is ALLOWED in mingw32
    template<typename TF> // THIS SHOULD GENERATE AN ERROR
    friend
    void*   t_other_class::operator new   ( size_t            arg1  ) ;

    // Examples of templating..
    template<typename TF>
    friend  /* IS templated in t_other_class*/
    void*   t_other_class::operator new   ( size_t            arg1
                                          , int               arg2
                                          ) ;
    template<typename TF>
    friend  /* IS NOT templated in t_other_class*/
    void*                  operator new   ( size_t            arg1
                                          , int               arg2
                                          , int               arg3
                                          ) ;
} ; 

/*
OK
====
windows
GNU C++ (GCC) version 4.8.2 (i686-w64-mingw32)
DUE
gcc version 4.8.3 20140228 (release) [ARM/embedded-4_8-branch revision 208322] (GNU Tools for ARM Embedded Processors)
Error
====
linux
GNU C++ (Ubuntu 4.8.4-2ubuntu1~14.04.3) version 4.8.4 (i686-linux-gnu)
*/   

我更新到最新的 Mingw,它现在正确报告了模板声明的滥用(根据其他编译器)。看起来旧版本的 mingw 是问题所在。