C++ 仅适用于智能指针的模板
嗨,我不确定这是否可能,因为我想问一下,因为可能有更好的方法来实现类似的事情,我不知道。 为了简单起见,我们只需考虑<代码> VectorT <代码>是C++ 仅适用于智能指针的模板,c++,templates,c++11,C++,Templates,C++11,嗨,我不确定这是否可能,因为我想问一下,因为可能有更好的方法来实现类似的事情,我不知道。 为了简单起见,我们只需考虑 VectorT 是 template<class T> class VectorT: private std::vector<T>` 模板 类:私有std::vector` 尝试我想要的东西是一种类似于 namespace detail { template<class SmartPtr> class MyClassVectorBase :
template<class T>
class VectorT: private std::vector<T>`
模板
类:私有std::vector`
尝试我想要的东西是一种类似于
namespace detail
{
template<class SmartPtr>
class MyClassVectorBase : public VectorT<SmartPtr>
{
public:
MyClassVectorBase() = default;
// all common functions of MyVectorView and MyVector
};
}
using MyClassVectorView = detail::MyClassVectorBase<nonstd::observer_ptr<SomeClass>>;
class MyVector : public detail::MyClassVectorBase<std::unique_ptr<SomeClass>>
{
// only functions related to the actual owner vector
};
名称空间详细信息
{
模板
类MyClassVectorBase:公共向量库
{
公众:
MyClassVectorBase()=默认值;
//MyVectorView和MyVector的所有常用函数
};
}
使用MyClassVectorView=detail::MyClassVectorBase;
类MyVector:公共详细信息::MyClassVectorBase
{
//仅与实际所有者向量相关的函数
};
我希望MyClassVectorBase只能在智能指针类型上模板化,并且只能接受SomeClass。
我认为这可能是专业化的,但我不知道这样的东西的语法是什么
template<class T, class SmartPtr>
class MyClassVectorBase : public VectorT<SmartPtr<T>>
{
};
template<SomeClass T, typename SmartPtr>
class MyClassVectorBase : public VectorT<SmartPtr<T>>
{
};
模板
类MyClassVectorBase:公共向量库
{
};
模板
类MyClassVectorBase:公共向量库
{
};
这样的事情可能吗
编辑:
好的,让我试着解释一下这一点以及背后的逻辑。我需要一堆食物。只有福,没有别的。
在一种情况下,类将是对象的所有者,并具有一些额外的函数。
因为它是所有者,所以它将是classMyClassVector:public-vecort
然后我必须对这些物体进行操作,但这些物体不会被拥有。
所有权是单一的,并且将始终比我将操作的对象更有效,因此不需要共享\u ptr
。
那么我猜我的类将是一个“视图类”MyClassVectorView:public-vecort
与其说是“观察者”,不如说是“原始ptr”,但这样做的目的更好。
现在MyClassVectorView
将具有与MyClassVector
相同的所有函数,这就是为什么我认为我将继承它
为此,我需要有一个基类,它将同时接受unique\u ptr
和observer\u ptr
。
这样我就可以避免重复,只要我能做到MyClassVector:public MyClassVectorView
备选方案是使用一个类并使用SFINAE检测模板参数是否为唯一的\u ptr,然后启用额外的函数。这将避免额外的继承。不确定您想要获得什么,但我怀疑您需要模板参数 我想您可以声明(但不定义)
MyClassVectorBase
接收单个模板typename参数
template <typename>
class MyClassVectorBase;
您的示例经过修改和集成(使用main()
和一个虚拟的observer\u ptr
)
#包括
#包括
#包括
#包括
名称空间NONSD
{
模板
结构观察者
{ };
}
模板
类别侵权
{
公众:
//公开必要的函数
私人:
std::向量容器;
};
结构Foo{
双x;
};
模板
类MyClassVectorBase;
//这个类应该只接受Foo的智能指针
模板
类MyClassVectorBase:公共向量库
{
公众:
MyClassVectorBase()=默认值;
void doSomething(){}
void doSomething2(){}
};
使用MyClassVectorView=MyClassVectorBase;
类MyVector:公共MyClassVectorBase
{
//函数仅用于该类,但仍继承所有MyClassVectorBase内容
};
int main()
{
}
您可以使用std::is_same显式检查上述注释中所需的智能指针,或者您也可以不定义基本模板,为SmartPtr提供模板专用化。您在这里似乎严重误用了继承。我想回答这个问题,但不清楚你到底想做什么。您的vecort
只是从std::vector
私下继承的。私有继承的错误远远多于正确,因此从一开始这就是一个相当糟糕的迹象。@MilesRout这只是为了简单起见。无论如何,std::vector的私有继承没有错,std::vector的公共继承有很多错。相反,我可以有一个向量facade,它是通过组合实现的,并公开所有必要的向量函数。最后,这将与我现在得到的完全相同,并且根本不会影响问题,因为我继承的是Vec侵权行为。关于std::vector
的私有继承并不简单,它有很多错误。几乎可以肯定,在这个问题中根本不应该使用任何继承。继承混淆了您的代码,不清楚您要做什么。听起来你真正想要的只是一些模板化的自由函数——我不明白你为什么需要任何类。这绝对是正确的,而且非常有效,所以我认为它是正确的。但是,我自己的类在为唯一的\u ptr添加删除器时确实遇到了问题。最后,这更容易,尽管我不知道它是否有任何问题模板类MyVector:public vectory{public:void doSomething1(){}void doSomething2(){};myvectorv1;MyVector v2代码>
template <template<typename...> class SmartPtr, typename Foo>
class MyClassVectorBase<SmartPtr<Foo>> : public VectorT<SmartPtr<Foo>>
{
public:
MyClassVectorBase() = default;
void doSomething(){}
void doSomething2(){}
};
template <template<typename...> class SmartPtr>
class MyClassVectorBase<SmartPtr<Foo>> : public VectorT<SmartPtr<Foo>>
{
public:
MyClassVectorBase() = default;
void doSomething(){}
void doSomething2(){}
};
#include <iostream>
#include <string>
#include <vector>
#include <memory>
namespace nonstd
{
template <typename T>
struct observer_ptr
{ };
}
template <class T>
class VectorT
{
public:
// expose nececssary functions
private :
std::vector<T> container_;
};
struct Foo{
double x;
};
template <typename>
class MyClassVectorBase;
// this class should only accept smart pointers of Foo
template <template<typename...> class SmartPtr, typename Foo>
class MyClassVectorBase<SmartPtr<Foo>> : public VectorT<SmartPtr<Foo>>
{
public:
MyClassVectorBase() = default;
void doSomething(){}
void doSomething2(){}
};
using MyClassVectorView = MyClassVectorBase<nonstd::observer_ptr<Foo>>;
class MyVector : public MyClassVectorBase<std::unique_ptr<Foo>>
{
// function only for this class but still inheriting all MyClassVectorBase stuff
};
int main ()
{
}