Search code examples
c++staticcrtpstatic-polymorphism

Curiously Recurring Template Pattern and statics in the base class


So thanks to this answer I'm looking at implementing my problem with CRTP. However I have a problem. In my static base class I have 2 sets of functions. One takes std::vectors and one takes a standard C-style array. So in the base class I define a static function that calls the non-std::vector function.

However when I derive from that base class I seem to no longer be able to access the public static function in the base class (Which I thought I could).

template< class Derived > class Base
{
public:
    static void Func( std::vector< float >& buffer )
    {
       Func( &buffer.front(), buffer.size() );
    }

    static void Func( float* pBuffer, int size )
    {
        Derived::Func( pBuffer, size );
    }
};

I then define the derived class as follows:

class Derived : public Base< Derived >
{
public:
    static void Func( float* pBuffer, int size )
    {
        // Do stuff
    }
};

However when I try to call the static function in the Base class:

Derived::Func( stlVec );

From the Derived class it throws a compilation error:

error C2665: 'main' : none of the 2 overloads could convert all the argument types
1>          c:\development\Base.h(706): could be 'void Func( float*, int )

I assumed that I would be able to call a public static defined in the base class from the Derived class. This appears not to be the case, however ... Can anyone suggest a workaround that doesn't mean having to implement the std::vector function in every one of my Derived classes?


Solution

  • Func in the derived class hides all base members with the same name. Use using declarative to bring the names from the base class into the derived class.

    class Derived : public Base< Derived >
    {
    public:
    
        using Base<Derived>::Func;
    
         //rest..
    };