gpt4 book ai didi

c++ - 使用 Curiously Recursive Template Pattern 在子类之间进行转换

转载 作者:行者123 更新时间:2023-11-30 04:02:12 25 4
gpt4 key购买 nike

给出:

  • 一个模板类 Base使用类型名参数 value_type ,
  • 它的 Curiously Recursive TemplatePattern模板“子类”DerivedFooDerivedBar , 和
  • 模板“子类”DerivedBarCodeDerivedBarDoge源自 DerivedBar ,

我怎么可能,或者我有没有可能:

  • 如图所示,实现每对三叶派生类型之间的转换(通过显式构造函数、赋值运算符、),以及
  • 禁用(通过编译器错误消息)图中未指定的转换?

图表:

Base -+---> DerivedFoo <-------+
+-+-> DerivedBar | // Three-way
+---> DerivedBarCode <-+ // conversion
+---> DerivedBarDoge <-+

场景抽象自:

MatrixBase -+---> DenseMatrix <------------+
+-+-> SparseMatrixBase |
+---> MatrixCSR <------------+ // Multiple-way
+---> MatrixCSC <------------+ // conversion
+---> MatrixModifiedCSR <----+
+---> MatrixModifiedCSC <----+

其中每个类都有至少一个公共(public)模板参数,typename value_type .

最小示范示例:

解释:

  • Lines 116...128lines 160...169 显示了我在模板类之外定义模板类的显式模板构造函数的尝试,以转换DerivedBarCode<value_type>DerivedBarDoge<value_type>进入DerivedFoo<value_type> .
  • 第 49 行包含静态断言,该断言会因图中未指定的所有转换而停止。
  • 删除 第 189...202 行 以及 第 116...128 行第 160...169 行 以成功编译和跑。

代码:

#include <iostream>

// ----------------------------------------------------------------------------
// Base
// ----------------------------------------------------------------------------
template <typename value_type, typename derived_type>
class Base
{
public:
// Pass along the value type.
typedef value_type value_type;
// Identify myself in the Curiously Recursive Template Pattern hierarchy.
typedef Base object_type;
// Delegate to derived types.
derived_type & asLeaf() { return static_cast<derived_type &>(*this); }

protected:
value_type m_Base;

public:
Base(value_type base = value_type()) : m_Base(base) {}

public:
// A delegated function
void Dump() { asLeaf().Dump(); }
};

// ----------------------------------------------------------------------------
// Base -----> Derived Foo
// ----------------------------------------------------------------------------
template <typename value_type>
class DerivedFoo : public Base < value_type, DerivedFoo<value_type> >
{
public:
// Identify myself in the CRTP hierarchy.
typedef DerivedFoo object_type;

protected:
value_type m_Foo;

public:
DerivedFoo(value_type base = value_type(), value_type foo = value_type()) :
Base < value_type, DerivedFoo<value_type> >(base), m_Foo(foo) {}

template <typename object_type_2>
DerivedFoo(object_type_2 const &other)
{
static_assert(false, "Non-specialized template constructor disabled.");
}

public:
// A possible implementation of the delegated function
void Dump()
{
std::cout << "DerivedFoo = [" << m_Base << ", " << m_Foo << "]\n";
}
};

// ----------------------------------------------------------------------------
// Base -+---> DerivedFoo -+ Two-way
// +---> DerivedBar -+ conversion
// ----------------------------------------------------------------------------
template <typename value_type, typename bar_type>
class DerivedBar : public Base < value_type, DerivedBar<value_type, bar_type> >
{
public:
// Pass along the derived bar type.
typedef bar_type bar_type;
// Identify myself in the CRTP hierarchy.
typedef DerivedBar object_type;
// Delegate to derived types.
bar_type & asLeaf() { return static_cast<bar_type &>(*this); }

protected:
value_type m_Bar;

public:
DerivedBar(value_type base = value_type(), value_type bar = value_type()) :
Base < value_type, DerivedBar<value_type, bar_type> >(base), m_Bar(bar)
{}

public:
void Dump() { asLeaf().Dump(); }
};

// ----------------------------------------------------------------------------
// Base -+---> DerivedFoo <-------+
// +-+-> DerivedBar <-------+ Three-way conversion
// +---> DerivedBarCode <-+
// ----------------------------------------------------------------------------
template <typename value_type>
class DerivedBarCode :
public DerivedBar < value_type, DerivedBarCode<value_type> >
{
public:
typedef DerivedBarCode object_type;

protected:
value_type m_Code = 8;

public:
DerivedBarCode(value_type base = value_type(),
value_type bar = value_type(), value_type code = value_type()) :
DerivedBar < value_type, DerivedBarCode<value_type> >(base, bar),
m_Code(code) {}

public:
void Dump()
{
std::cout << "DerivedBarCode = ["
<< m_Base << ", " << m_Bar << ", " << m_Code << "]\n";
}
};

// DerivedBarCode => DerivedFoo
// Example of what I'm trying to do:
template <typename value_type>
template <>
DerivedFoo<value_type>::DerivedFoo(
typename DerivedBarCode<value_type>::object_type const &other)
{
m_Base = other.m_Base;
m_Foo = other.m_Foo;
// There may be other calculations, e.g. replacing the line above with:
// m_Foo = other.m_Foo + other.m_Code;
std::cout << "m_Code = " << other.m_Code << '\n';
}

// ----------------------------------------------------------------------------
// Base -+---> DerivedFoo <-------+
// +-+-> DerivedBar <-------+ Four-way
// +---> DerivedBarCode <-+ conversion
// +---> DerivedBarDoge <-+
// ----------------------------------------------------------------------------
template <typename value_type>
class DerivedBarDoge :
public DerivedBar < DerivedBarDoge<value_type>, value_type >
{
public:
typedef DerivedBarDoge object_type;

protected:
value_type m_Doge;

public:
DerivedBarDoge(value_type base = value_type(),
value_type bar = value_type(), value_type dance = value_type()) :
DerivedBar < DerivedBarDoge<value_type>, value_type >(base, bar),
m_Code(dance) {}

void Dump()
{
std::cout << "DerivedBarDoge = ["
<< m_Base << ", " << m_Bar << ", " << m_Doge << "]\n";
}

};

// DerivedBarDoge => DerivedFoo
// Another attempt.
template <typename value_type>
template <>
DerivedFoo<value_type>::DerivedFoo(DerivedBarDoge<value_type> const &other)
{
m_Base = other.m_Base;
m_Foo = other.m_Foo;
std::cout << "m_Doge = " << other.m_Doge << '\n';
}


int main()
{
DerivedFoo<double> foo(1.0, 2.0);
foo.Dump();
// Output:
// DerivedFoo = [1, 2];

DerivedBarCode<double> barcode(4.0, 8.0, 16.0);
barcode.Dump();
// Output:
// DerivedBarCode = [4, 8, 16];

DerivedBarDoge<double> bardoge(32.0, 64.0, 128.0);
bardoge.Dump();
// Output:
// DerivedBarDoge = [32, 64, 128];

DerivedFoo<double> converted(barcode);
converted.Dump();
// Expected output:
// DerivedFoo = [4, 8];

converted = bardoge;
converted.Dump();
// Expected output:
// DerivedFoo = [32, 64];

barcode = foo;
barcode.Dump();
// Expected output:
// DerivedFoo = [1, 2, 16];

// system("pause");
return 0;
}

最佳答案

CRTP 基会将它们的类名注入(inject)到派生类中。因此,如果每个 CRTP 基都知道派生类型是 derived_type , 然后去另一个基地 other_base< derived_type >是一个简单的问题

static_cast< typename derived_type::other_base & >(
static_cast< derived_type & >( * this ) );

如果 CRTP 基础 mixin 可以由多个模板中的任何一个实现,那么它应该将 typedef 添加到它自己的类型中,以识别它是哪个 mixin。然后将其用于 ::other_base .

要添加转换,只需将此类转换放入转换运算符模板中即可。在实际尝试之前,请务必使用 SFINAE 对转换进行清理。

template< typename t,
std::enable_if_t< std::is_base_of_v< t, derived_type > > * = nullptr >
operator t & ()
{ return etc; }

您可以另外/交替地使用成员 typedef 标记所有合适的 mixins,而 SFINAE 会检查该标记。要仅选择图中的转换,请将标记升级为 bool 元函数。

关于c++ - 使用 Curiously Recursive Template Pattern 在子类之间进行转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25304741/

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