相关疑难解决方法(0)

函数的模板推导基于其返回类型?

我希望能够使用模板推导来实现以下目标:

GCPtr<A> ptr1 = GC::Allocate();
GCPtr<B> ptr2 = GC::Allocate();
Run Code Online (Sandbox Code Playgroud)

而不是(我现在拥有的):

GCPtr<A> ptr1 = GC::Allocate<A>();
GCPtr<B> ptr2 = GC::Allocate<B>();
Run Code Online (Sandbox Code Playgroud)

我目前的Allocate功能如下所示:

class GC
{
public:
    template <typename T>
    static GCPtr<T> Allocate();
};
Run Code Online (Sandbox Code Playgroud)

这将是可能敲掉多余的<A><B>

c++ templates template-argument-deduction

27
推荐指数
3
解决办法
1万
查看次数