开发者

Problem with GCC calling static templates functions in templated parent class

开发者 https://www.devze.com 2022-12-26 17:41 出处:网络
I have some code that compiles and runs on MSVC++ but will not compile on GCC.I have made a test snippet that follows.My goal was to move the static method from BFSMask to BFSMaskSized.Can someone exp

I have some code that compiles and runs on MSVC++ but will not compile on GCC. I have made a test snippet that follows. My goal was to move the static method from BFSMask to BFSMaskSized. Can someone explain what is going on with the errors (esp. the weird 'operator<' error)? Thank you.

In the case of both #defines are 0, then the code compiles on GCC.

#define DOESNT_COMPILE_WITH_GCC     0
#define FUNCTION_IN_PARENT          0

I get errors if I change either #define to 1. Here are the errors I see.

#define DOESNT_COMPILE_WITH_GCC     0
#define FUNCTION_IN_PARENT          1
Test.cpp: In static member function 'static typename Snapper::BFSMask<T>::T_Parent::T_SINT Snapper::BFSMask<T>::Create_NEZ(TCMP)':
Test.cpp(492): error: 'CreateMaskFromHighBitSized' was not declared in this scope

#define DOESNT_COMPILE_WITH_GCC     1
#define FUNCTION_IN_PARENT          0
Test.cpp: In static member function 'static typename Snapper::BFSMask<T>::T_Parent::T_SINT Snapper::BFSMask<T>::Create_NEZ(TCMP) [with TCMP = int, T = int]':
Test.cpp(500):   instantiated from 'TVAL Snapper::BFWrappedInc(TVAL, TVAL, TVAL) [with TVAL = int]'
Test.cpp(508):   instantiated from here
Test.cpp(490): error: invalid operands of types '<unresolved overloaded function type>' and 'unsigned int' to binary 'operator<'

#define DOESNT_COMPILE_WITH_GCC     1
#define FUNCTION_IN_PARENT          1
Test.cpp: In static member function 'static typename Snapper::BFSMask<T>::T_Parent::T_SINT Snapper::BFSMask<T>::Create_NEZ(TCMP) [with TCMP = int, T = int]':
Test.cpp(500):   instantiated from 'TVAL Snapper::BFWrappedInc(TVAL, TVAL, TVAL) [with TVAL = int]'
Test.cpp(508):   instantiated from here
Test.cpp(490): error: invalid operands of types '<unresolved overloaded function type>' and 'unsigned int' to binary 'operator<'

Here is the code

namespace Snapper
{
#define DOESNT_COMPILE_WITH_GCC     0
#define FUNCTION_IN_PARENT          0

    // MASK TYPES
    // NEZ  - Not Equal to Zero
    #define BFSMASK_NEZ(A)      ( ( A ) | ( 0 - A ) )
    #define BFSELECT_MASK(MASK,VTRUE,VFALSE)    ( ((MASK)&(VTRUE)) | ((~(MASK))&(VFALSE)) )
    template<typename TVAL> TVAL BFSelect_MASK(TVAL MASK,TVAL VTRUE,TVAL VFALSE)
    { return(BFSELECT_MASK(MASK,VTRUE,VFALSE)); }

    //-----------------------------------------------------------------------------
    // Branch Free Helpers

    template<int BYTESIZE> struct BFSMaskBase {};
    template<> struct BFSMaskBase<2>
    {
        typedef UINT16  T_UINT;
        typedef SINT16  T_SINT;
    };
    template<> struct BFSMaskBase<4>
    {
        typedef UINT32  T_UINT;
        typedef SINT32  T_SINT;
    };
    template<int BYTESIZE> struct BFSMaskSized : public BFSMaskB开发者_如何学JAVAase<BYTESIZE>
    {
        static const int SizeBytes      = BYTESIZE;
        static const int SizeBits       = SizeBytes*8;
        static const int MaskShift      = SizeBits-1;
        typedef typename BFSMaskBase<BYTESIZE>::T_UINT      T_UINT;
        typedef typename BFSMaskBase<BYTESIZE>::T_SINT      T_SINT;

#if FUNCTION_IN_PARENT
        template<int N> static T_SINT CreateMaskFromHighBitSized(typename BFSMaskBase<N>::T_SINT inmask);
#endif
    };

    template<typename T> struct BFSMask : public BFSMaskSized<sizeof(T)>
    {
        //  BFSMask = -1 (all bits set)

        typedef BFSMask<T>                      T_This;
        // "Import" the Parent Class
        typedef BFSMaskSized<sizeof(T)>         T_Parent;
        typedef typename T_Parent::T_SINT       T_SINT;

#if FUNCTION_IN_PARENT
        typedef T_Parent    T_MaskGen;
#else
        typedef T_This      T_MaskGen;
        template<int N> static T_SINT CreateMaskFromHighBitSized(typename BFSMaskSized<N>::T_SINT inmask);
#endif

        template<typename TCMP> static T_SINT Create_NEZ(TCMP A)
        {
            //ReDefineType(const typename BFSMask<TCMP>::T_SINT,SA,A);
            //const typename BFSMask<TCMP>::T_SINT cmpmask = BFSMASK_NEZ(SA);
            const typename BFSMask<TCMP>::T_SINT cmpmask = BFSMASK_NEZ(A);
#if DOESNT_COMPILE_WITH_GCC
            return(T_MaskGen::CreateMaskFromHighBitSized<sizeof(TCMP)>(cmpmask));
#else
            return(CreateMaskFromHighBitSized<sizeof(TCMP)>(cmpmask));
#endif
        }
    };

    template<typename TVAL> TVAL BFWrappedInc(TVAL x,TVAL minval,TVAL maxval)
    {
        const TVAL diff = maxval-x;
        const TVAL mask = BFSMask<TVAL>::Create_NEZ(diff);
        const TVAL incx = x + 1;
        return(BFSelect_MASK(mask,incx,minval));
    }

    SINT32 currentsnap = 0;
    SINT32 SetSnapshot()
    {
        currentsnap=BFWrappedInc<SINT32>(currentsnap,0,20);
        return(currentsnap);
    }
}


Since CreateMaskFromHighBitSized doesn't depend on the template parameter of the class it's not a dependent name and the compiler expects to find it without looking at the templated base class. Therefore T_MaskGen:: has to be specified if the function should be found in the base class.

When the name is explicitly qualified as T_MaskGen::CreateMaskFromHighBitSized it isn't obvious to the compiler that this always will be a template. The template keyword is necessary to make that clear (in the same way typename is often used in these situations):

return(T_MaskGen::template CreateMaskFromHighBitSized<sizeof(TCMP)>(cmpmask));

This version of the call should work for both definitions of FUNCTION_IN_PARENT.


Expanding on the answer given by sth: this is explained in C++ Templates: The Complete Guide like this, in section 9.3.3 Dependent Names of Templates:

In general, a C++ compiler is required to treat a < following the name of a template as the beginning of a template argument list; otherwise it is a "less than" operator. As is the case with type names, a compiler has to assume that a dependent name does not refer to a template unless the programmer provides extra information using the keyword template[.]

0

精彩评论

暂无评论...
验证码 换一张
取 消