我发现另一个模板作为参数传递给模板的示例:
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 编译器提示“分配器:不明确的符号”
这是编译器问题还是这段代码有问题?
最佳答案
你很可能有邪恶:
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;
}
关于c++ - 模板的模板参数 - 不明确的符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15175613/