c++ - 用作模板的类中的静态常量变量

标签 c++ templates

我在使用具有一些static const 变量的类作为模板化类的模板时遇到问题。 这是我的普通类(只是一个 header 文件):

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

    inline void guardFront() const {};
};

这就是我想要使用它的方式(也是一个 header 文件):

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;
};

这很好用:m_boundsGuard.guardFront(); 但是这个 m_boundsGuard::sizeFront 给我错误。 这是完整的错误:

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

最佳答案

m_boundsGuard 不是类或命名空间。正确的版本是:

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

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

关于c++ - 用作模板的类中的静态常量变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22128615/

相关文章:

c++ - 为什么 SDL 在最后一个窗口关闭时不发出 SDL_QUIT

c++ - 函数指针的函数模板特化

templates - Ember 。实时上传模板

c++ - 从函数指针创建 boost::function 的跨平台方式

c++ - 在 CryptoAPI 中识别 CSP 提供者

c++ - 为什么不能在 if 条件下比较两个字符串?

C++ VS2008 - 类模板实例化的奇怪错误

c++ - 线程过程中的线程同步问题

c++ - 在具有指针参数的模板实例化之间进行选择

c++ - 导出包含它们的模板时未创建源文件/头文件