简体   繁体   中英

template friend operator new mingw32

My understanding is that operator new cannot be templated (when declared using the parameters I have used.. (size_t)

The following compiles OK on mingw32 and arduino DUE, but not on other platforms eg linux.

Is there a bug in mingw32, or should this be allowed on other targets too ?

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)
*/   

I updated to latest Mingw and it now correctly reports the misuse of template declaration (as per other compilers). Looks like old version of mingw was the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM