gpt4 book ai didi

c++ - 仅适用于智能指针的模板

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:45:24 25 4
gpt4 key购买 nike

您好,我不确定这是否可行,但我想问问,因为可能有更好的方法来实现我不知道的类似事情。为简单起见,我们只考虑 VectorT

template<class T>
class VectorT: private std::vector<T>`

尝试实现我想要的东西。

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 可以只在智能指针类型上进行模板化,并且只接受 SomeClass。我认为特化可能是可能的,但我不知道类似的东西的语法是什么

template<class T, class SmartPtr>
class MyClassVectorBase : public VectorT<SmartPtr<T>>
{
};

template<SomeClass T, typename SmartPtr>
class MyClassVectorBase : public VectorT<SmartPtr<T>>
{
};

这样的事情甚至可能吗?

已编辑:好的,让我试着解释一下这个及其背后的逻辑。我需要有一个 Foo 对象的 VectorT。只有 Foo,没有别的。在一种情况下,该类将是对象的所有者并具有一些额外的功能。因为它是所有者,所以它将是类 MyClassVector : public VectorT<std::unique_ptr<Foo>>然后我必须以某种方式对这些对象进行操作,但这些对象不会被拥有。所有权是单一的,并且永远比我将操作的对象长寿,所以不需要 shared_ptr .那么我想我的类(class)将是“查看类(class)”MyClassVectorView : public VectorT<std::observer_ptr<Foo>>而不是 observer_ptr也可以说是原始 ptr,但它的意图更好。现在MyClassVectorView将与 MyClassVector 具有所有相同的功能这就是为什么我认为我会继承它。

为此,我需要有一个基类可以同时接受 unique_ptrobserver_ptr .然后我可以避免重复,只要我能做 MyClassVector : public MyClassVectorView<std::unique_ptr<Foo>>

如果模板参数是 unique_ptr,替代方案将有一个类并使用 SFINAE 进行检测,然后启用额外的功能。这将避免额外的继承。

最佳答案

不确定您想要获取什么,但我怀疑您需要模板模板参数。

我想您可以声明(但不定义)MyClassVectorBase 接收单个模板类型名参数

template <typename>
class MyClassVectorBase;

然后定义一个基于模板的特化模板;像

template <template<typename...> class SmartPtr, typename Foo>
class MyClassVectorBase<SmartPtr<Foo>> : public VectorT<SmartPtr<Foo>>
{
public:
MyClassVectorBase() = default;

void doSomething(){}
void doSomething2(){}
};

如果Foo不是模板参数,而是Foo struct,你可以这样写

template <template<typename...> class SmartPtr>
class MyClassVectorBase<SmartPtr<Foo>> : public VectorT<SmartPtr<Foo>>
{
public:
MyClassVectorBase() = default;

void doSomething(){}
void doSomething2(){}
};

您的示例已修改并集成(使用 main() 和虚拟 observer_ptr)

#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 ()
{
}

关于c++ - 仅适用于智能指针的模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39801826/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com