C++ 如何使用参数调用类模板函数

C++ 如何使用参数调用类模板函数,c++,C++,在尝试将模板参数传递给函数调用时,我不断得到预期的主表达式 我的错误如下所示: In PtrAllocator<T, Pool>::allocate(PtrAllocator<T, Pool>::size_type, const void*)': expected primary-expression before '>' token In PtrAllocator<T, Pool>::max_size() const': expected prima

在尝试将模板参数传递给函数调用时,我不断得到预期的主表达式

我的错误如下所示:

In PtrAllocator<T, Pool>::allocate(PtrAllocator<T, Pool>::size_type, const void*)': expected primary-expression before '>' token

In PtrAllocator<T, Pool>::max_size() const': expected primary-expression before '>' token
template<typename T, typename Pool>
class PtrAllocator : public BasicAllocator<T>
{
    private:
        Pool pool;

    public:
        typedef typename BasicAllocator<T>::pointer pointer;
        typedef typename BasicAllocator<T>::size_type size_type;
        typedef typename BasicAllocator<T>::value_type value_type;

        template<typename U>
        struct rebind {typedef PtrAllocator<U, Pool> other;};

        PtrAllocator(Pool&& pool) : pool(pool) {}

        pointer allocate(size_type n, const void* hint = 0) {return static_cast<pointer>(pool.allocate<T>(n, hint));}
        void deallocate(void* ptr, size_type n) {pool.deallocate(static_cast<pointer>(ptr), n);}
        size_type max_size() const {return pool.max_size<T>();}
};

class Pool
{
    public:
        template<typename T>
        void* allocate(std::size_t n, const void* hint = 0) {return ::operator new(n * sizeof(T));}

        template<typename T>
        void deallocate(T* ptr, std::size_t n) {::operator delete(ptr);}

        template<typename T>
        std::size_t max_size() const {return std::numeric_limits<std::size_t>::max() / sizeof(T);}
};

int main()
{
    PtrAllocator<int, Pool> alloc = PtrAllocator<int, Pool>(Pool());
    std::vector<int, PtrAllocator<int, Pool>> v(alloc);
    v.resize(1000); //this line is causing the error.
}
PtrAllocator::allocate(PtrAllocator::size_type,const void*)中的
:在“>”标记之前应为主表达式
在PtrAllocator::max_size()const中:在“>”标记之前应该有主表达式
我的代码如下所示:

In PtrAllocator<T, Pool>::allocate(PtrAllocator<T, Pool>::size_type, const void*)': expected primary-expression before '>' token

In PtrAllocator<T, Pool>::max_size() const': expected primary-expression before '>' token
template<typename T, typename Pool>
class PtrAllocator : public BasicAllocator<T>
{
    private:
        Pool pool;

    public:
        typedef typename BasicAllocator<T>::pointer pointer;
        typedef typename BasicAllocator<T>::size_type size_type;
        typedef typename BasicAllocator<T>::value_type value_type;

        template<typename U>
        struct rebind {typedef PtrAllocator<U, Pool> other;};

        PtrAllocator(Pool&& pool) : pool(pool) {}

        pointer allocate(size_type n, const void* hint = 0) {return static_cast<pointer>(pool.allocate<T>(n, hint));}
        void deallocate(void* ptr, size_type n) {pool.deallocate(static_cast<pointer>(ptr), n);}
        size_type max_size() const {return pool.max_size<T>();}
};

class Pool
{
    public:
        template<typename T>
        void* allocate(std::size_t n, const void* hint = 0) {return ::operator new(n * sizeof(T));}

        template<typename T>
        void deallocate(T* ptr, std::size_t n) {::operator delete(ptr);}

        template<typename T>
        std::size_t max_size() const {return std::numeric_limits<std::size_t>::max() / sizeof(T);}
};

int main()
{
    PtrAllocator<int, Pool> alloc = PtrAllocator<int, Pool>(Pool());
    std::vector<int, PtrAllocator<int, Pool>> v(alloc);
    v.resize(1000); //this line is causing the error.
}
模板
类PtrAllocator:公共基本定位器
{
私人:
游泳池;
公众:
typedef typename基本定位符::指针指针;
typedef typename基本定位符::大小\类型大小\类型;
typedef typename BasicAllocator::value\u type value\u type;
模板
结构重新绑定{typedef PtrAllocator other;};
PtrAllocator(Pool&&Pool):Pool(Pool){}
指针分配(size_type n,const void*hint=0){return static_cast(pool.allocate(n,hint));}
void deallocate(void*ptr,size_type n){pool.deallocate(static_cast(ptr),n);}
size_type max_size()常量{return pool.max_size();}
};
班级池
{
公众:
模板
void*allocate(std::size_t n,const void*hint=0){return::operator new(n*sizeof(t));}
模板
无效解除分配(T*ptr,std::size_T n){::运算符删除(ptr);}
模板
std::size_t max_size()常量{返回std::numeric_limits::max()/sizeof(t);}
};
int main()
{
PtrAllocator alloc=PtrAllocator(Pool());
std::载体v(alloc);
v、 调整大小(1000);//此行导致错误。
}
PtrAllocator::allocate
调用
Pool::allocate
时会发生错误。同样的事情发生在
max_size
上,但在
deallocate
上没有发生。
你知道为什么不让我指定模板参数吗?

你需要告诉编译器,
allocate
是一个模板,否则表达式将不明确:

pool.template allocate<T>(n, hint)

我用星号标记的那条线和你的表达完全一样,但它的意思完全不同。它实际上相当于
(pool.allocate(n,hint)
。也就是说,
不再是模板参数限定符——它们是关系运算符!我正在比较数据成员
池。allocate
T
您需要告诉编译器
allocate
是一个模板,否则表达式将不明确:

pool.template allocate<T>(n, hint)

我用星号标记的那条线和你的表达完全一样,但它的意思完全不同。它实际上相当于
(pool.allocate(n,hint)
。也就是说,
不再是模板参数限定符——它们是关系运算符!我正在比较数据成员
池.allocate
T

我一生中从未见过这种语法。模板下面的点运算符..:o。非常感谢。这解决了我的问题。我会尽快接受这个答案。我也将你给我的链接添加到书签中。@CantChooseServerNames我添加了一个示例来说明为什么这个表达式是不明确的。我一生中从未见过这种语法。模板下面的点运算符..:o。非常感谢。这解决了我的问题。我会尽快接受这个答案。我也给了我的链接加了书签。@CantChooseUsernames我添加了一个例子来演示为什么这个表达式是不明确的。