puelo
puelo

Reputation: 6057

static const variable inside a class used as template

I have a problem using a class which has some static const variables as a template for a templated class. This my normal class (Just a header file):

class NoBoundsChecking
{
public:
    static const size_t sizeFront = 0;
    static const size_t sizeBack = 0;

    inline void guardFront() const {};
};

This is how i want to use it (also a header file):

template<class BoundsCheckingPolicy>
class MemoryArena
{
public:

    MemoryArena()
    {
    }

    void* allocate(size_t size, size_t alignment, int line, char* file)
    {
        size_t boundedSize = m_boundsGuard::sizeFront + size + m_boundsGuard::sizeBack;
        m_boundsGuard.guardFront();
    }

private:
    BoundsCheckingPolicy    m_boundsGuard;
};

This works fine: m_boundsGuard.guardFront(); But this m_boundsGuard::sizeFront gives me errors. Here is the complete error:

error C2653: 'm_boundsGuard' : is not a class or namespace name
1>e:\...\memorymanager.h(102) : while compiling class template member function 'void *MemoryArena<NoBoundsChecking>::allocate(size_t,size_t,int,char *)'
1>e:\...\main.cpp(21) : see reference to function template instantiation 'void *MemoryArena<NoBoundsChecking>::allocate(size_t,size_t,int,char *)' being compiled
1>e:\...\main.cpp(19) : see reference to class template instantiation 'MemoryArena<NoBoundsChecking>' being compiled
1>e:\...\memorymanager.h(111): error C2065: 'sizeFront' : undeclared identifier

Upvotes: 0

Views: 123

Answers (2)

fasked
fasked

Reputation: 3645

m_boundsGuard is not class or namespace. Correct versions are:

// Using dot
size_t boundedSize = m_boundsGuard.sizeFront + size + m_boundsGuard.sizeBack;

// Using class
size_t boundedSize = BoundsCheckingPolicy::sizeFront + size + BoundsCheckingPolicy::sizeBack;

Upvotes: 4

Christian Hackl
Christian Hackl

Reputation: 27548

You are trying to access static members via an object, rather than the class. Try this:

size_t boundedSize = BoundsCheckingPolicy::sizeFront + size + BoundsCheckingPolicy::sizeBack;

Upvotes: 2

Related Questions