gpt4 book ai didi

C++ 将强类型基类与 CRTP 和返回值类型推导混合

转载 作者:太空狗 更新时间:2023-10-29 23:15:25 26 4
gpt4 key购买 nike

我在类层次结构中遇到了一些概念性问题,其中 Base 类依赖于固定标量类型 T ,但派生的 CRTP 类使用返回值类型推导。

例如,考虑以下类层次结构:

template<typename ... Args> struct VectorBase;

template<typename T>
struct VectorBase<T>
{
virtual T eval(int) const = 0;
auto operator[](int i) {return this->eval(i);}
};

template<typename T, typename Derived>
struct VectorBase<T, Derived> : public VectorBase<T>
{
virtual T eval(int i) const override final { return this->operator[](i); }
auto operator[](int i) const
{
return static_cast<Derived const&>(*this).operator[](i);
}
};

template<typename T>
struct Vector : public VectorBase<T, Vector<T> >
{
//just for code shortness,
//in reality there is a container which returns the corresponding elements
auto operator[](int i) const { return T{}; }
};

template<typename VectorType>
struct SomeTransformation : public VectorBase< /* ... what to write here generically? */ double, SomeTransformation<VectorType> >
{
VectorType const& v;
SomeTransformation(VectorType const& _v) : v(_v) {}
auto operator[](int i) const
{
//do something with vector v and return i-th element, e.g.
return v[i]*0.1;
}
};

DEMO

现在,给定一个值类型为 int 的特定 vector ,比如说,一个人可以申请 SomeTransformation并获得值类型的 vector double .此外,我可以确定 SomeTransformation源自 VectorBase<double> ,例如,我不能错误地将它分配给 VectorBase<int> -指针:

int main()
{
Vector<int> v;
std::cout<<typeid(decltype(v[0])).name()<<std::endl; //prints "i" for int

auto u = SomeTransformation<decltype(v)>(v);
std::cout<<typeid(decltype(u[0])).name()<<std::endl; //prints "d" for double

//works
std::unique_ptr<VectorBase<double> > ud = std::make_unique<SomeTransformation<decltype(v)> >(v);

//gives a compile-time error, which is good
//std::unique_ptr<VectorBase<int> > ui = std::make_unique<SomeTransformation<decltype(v)> >(v);
}

现在的问题是:在 SomeTransformation 的标量类型参数中,我写的地方 /* ... what to write here generically? */ , 我真的很想写类似的东西

template<typename VectorType>
struct SomeTransformation :
public VectorBase<decltype(std::declval<SomeTransformation<VectorType> >().operator[](0)), SomeTransformation<VectorType> >
{
//...
};

为了自动推断转换的值类型并将该类型传播到基类。然而,这似乎不起作用,我认为这是因为基类在派生类之前被实例化......所以我想要推断类型的类还不存在。

有什么方法可以在不破坏继承层次结构的情况下获得这种行为?

最佳答案

我自己想出了一个可能的替代方案,并想提出来进行讨论。

例如,可以将类型参数 T 添加到派生类,然后使用虚拟类型来实例化此类一次。这样,就可以推断出所创建类的返回类型,用于下一步实例化要真正使用的类。

例子:

namespace detail
{
template<typename T, typename VectorType>
struct SomeTransformation :
public VectorBase<T, SomeTransformation<T, VectorType> >
{
//the same as above
};
}

struct DummyType
{
//make any type convertible to DummyType
template<typename T> DummyType(T const&) {}
};

template<typename VectorType>
using SomeTransformationValueType =
decltype(std::declval<detail::SomeTransformation<DummyType, VectorType> >().operator[](0));

template<typename VectorType>
using SomeTransformation =
typename detail::SomeTransformation<SomeTransformationValueType<VectorType>, VectorType>;

DEMO

关于C++ 将强类型基类与 CRTP 和返回值类型推导混合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29305965/

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