1
我有这个功能为偏特正确的语法与智能指针
template<class A, class B>
std::shared_ptr<A> Foo(const B& obj) { ... }
我想提供一个方便的功能也得到智能指针(shared_ptr
或unique_ptr
),而不是引用。事情是这样的:
template<class A, class B>
std::shared_ptr<A> Foo(const std::shared_ptr<B>& obj) {
return Foo<A>(const_cast<const B&>(*obj));
}
它只能像我是否超载Foo
得到shared_ptr
作为参数。不过,我想把它写成部分专业化。我也试过
template<class A>
template<class B>
std::shared_ptr<A> Foo(const std::shared_ptr<B>& obj) { ... }
这个部分专业化的正确语法是什么?
功能模板不能部分专用。把它作为一个带有静态成员函数的类模板和一个转发给这个类的通用函数模板来写。 – Andrew
@Andrew超载应该足够了(由于部分排序) –
@PiotrSkotnicki确实应该。 – Andrew