gpt4 book ai didi

c++ - CRTP:根据派生类内容启用基类中的方法

转载 作者:行者123 更新时间:2023-11-30 05:16:44 28 4
gpt4 key购买 nike

有没有办法从 CRTP 基类查询派生类的内容,与 SFINAE 一起使用来启用或禁用基类方法?

我想要完成的可能如下所示:

template<typename Derived>
struct base
{
struct foo {};
struct bar {};

void dispatch(int i)
{
switch (i) {
case 0: dispatch(foo{}); break;
case 1: dispatch(bar{}); break;
default: break;
}
}

// catch all for disabled methods
template<typename T> void dispatch(T const&) {}

std::enable_if</* magic that checks if there is in fact Derived::foo(foo) */>
dispatch(foo f)
{
static_cast<Derived*>(this)->foo(f);
}
std::enable_if</* magic that checks if there is in fact Derived::bar(bar) */>
dispatch(bar b)
{
static_cast<Derived*>(this)->bar(b);
}
};

struct derived: public base<derived>
{
// only foo in this one
void foo(foo) { std::cout << "foo()\n"; }
};

简单地尝试在 enable_if 中使用 Derived::foo 会导致错误,引用不完整类(派生类)的无效使用。

最佳答案

Is there a way to query derived class' contents from a CRTP base class, to use with SFINAE to enable or disable base class methods?

是的,是的。它遵循一个最小的工作示例:

#include<iostream>

template<typename D>
class base {
template<typename T = D>
auto dispatch(int) -> decltype(std::declval<T>().foo(), void()) {
static_cast<T*>(this)->foo();
}

void dispatch(char) {
std::cout << "base" << std::endl;
}

public:
void dispatch() {
dispatch(0);
}
};

struct derived1: base<derived1> {
void foo() {
std::cout << "derived1" << std::endl;
}
};

struct derived2: base<derived2> {};

int main() {
derived1 d1;
derived2 d2;
d1.dispatch();
d2.dispatch();
}

添加要转发的参数很简单,我希望示例尽可能简单。
查看它在 wandbox 上运行.

从上面的代码片段可以看出,基本思想是使用标记分派(dispatch)和重载方法来启用或禁用基类中的方法,如果派生类中存在则使用派生类中的方法。

Simply trying to use Derived::foo inside enable_if results in an error citing invalid use of an incomplete class (derived).

那是因为当您尝试使用 Derived 时,它实际上是不完整的。标准说:

A class is considered a completely-defined object type (or complete type) at the closing } of the class-specifier.

在您的例子中,派生类有一个基类模板,并且由于显而易见的原因,在后者的实例化期间前者不是完整类型。
此外,Derived 不是您的 sfinae 表达式中的实际类型,并且(让我说)sfinae 在这种情况下不起作用。这就是我在示例中执行以下操作的原因:

template<typename T = D>
auto dispatch(int) -> decltype(std::declval<T>().foo(), void()) {
static_cast<T*>(this)->foo();
}

当然,这样使用的decltype也是一个sfinae表达式。如果您愿意,可以使用 std::enable_if_t 做类似的事情。我发现这个版本更易于阅读和理解。


也就是说,您可以通过虚拟方法获得相同的结果。如果您没有充分的理由不这样做,请使用它。


为了完整起见,您的示例已更新为上述技术:

#include<iostream>

template<typename Derived>
struct base
{
struct foo {};
struct bar {};

void dispatch(int i)
{
switch (i) {
case 0: dispatch(0, foo{}); break;
case 1: dispatch(0, bar{}); break;
default: break;
}
}

template<typename T>
void dispatch(char, T const&) {}

template<typename D = Derived>
auto dispatch(int, foo f)
-> decltype(std::declval<D>().foo(f), void())
{
static_cast<D*>(this)->foo(f);
}

template<typename D = Derived>
auto dispatch(int, bar b)
-> decltype(std::declval<D>().bar(b), void())
{
static_cast<D*>(this)->bar(b);
}
};

struct derived: public base<derived>
{
void foo(foo) { std::cout << "foo" << std::endl; }
};

int main() {
derived d;
d.dispatch(0);
d.dispatch(1);
}

查看 wandbox .

关于c++ - CRTP:根据派生类内容启用基类中的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42514100/

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