2

我发现这个作为参数传递给模板的另一个模板的例子:

template<template<typename T> class AllocatePolicy>
struct Pool {
    void allocate(size_t n) {
        int *p = AllocatePolicy<int>::allocate(n);
    }
};


template<typename T> 
    struct allocator { static T * allocate(size_t n) { return 0; } };


int main()
{
    // pass the template "allocator" as argument. 

    Pool<allocator> test;



    return 0;
}

这对我来说似乎完全合理,但 MSVC2012 编译器抱怨“分配器:不明确的符号”

这是编译器问题还是这段代码有问题?

4

1 回答 1

2

你很可能有一个邪恶:

using namespace std;

在您的代码中的某处,这使您的类模板与标准分配器allocator发生冲突。std::allocator

例如,除非您注释包含 using 指令的行,否则此代码不会编译:

#include <memory>

// Try commenting this!
using namespace std;

template<template<typename T> class AllocatePolicy>
struct Pool {
    void allocate(std::size_t n) {
        int *p = AllocatePolicy<int>::allocate(n);
    }
};

template<typename T>
struct allocator { static T * allocate(std::size_t n) { return 0; } };

int main()
{
    Pool<allocator> test;
}
于 2013-03-02T14:34:32.690 回答