将虚拟功能放入家庭
Putting virtual functions into a family
给定
class A {
public:
virtual int foo (int) const = 0;
virtual void bar (char, double) const = 0;
};
class B : public A {
virtual int foo (int) const {std::cout << "B::foo() called.n"; return 3;}
virtual void bar () const {std::cout << "B::bar() called.n";}
};
class C : public B {
virtual int foo (int) const {std::cout << "C::foo() called.n"; return 8;}
virtual void bar (char, double) const {std::cout << "C::bar() called.n";}
};
我想把foo
和bar
(以及A
的其他虚拟函数(放入一个函数的模板族中。以下是我到目前为止的想法:
#include <iostream>
enum Enum {Foo, Bar};
template <Enum> struct EnumTraits;
template <> struct EnumTraits<Foo> { using return_type = int; };
template <> struct EnumTraits<Bar> { using return_type = void; };
class A {
template <Enum> class Execute;
public:
virtual int foo (int) const = 0;
virtual void bar (char, double) const = 0;
template <Enum E, typename... Args>
typename EnumTraits<E>::return_type execute(Args&&... args) const {
return Execute<E>(this)(std::forward<Args>(args)...);
}
};
template <>
class A::Execute<Foo> {
const A* a;
public:
Execute (const A* a_) : a(a_) {}
template <typename... Args>
int operator()(Args&&... args) const {return a->foo(std::forward<Args>(args)...);}
};
template <>
class A::Execute<Bar> {
const A* a;
public:
Execute (const A* a_) : a(a_) {}
template <typename... Args>
void operator()(Args&&... args) const {a->bar(std::forward<Args>(args)...);}
};
class B : public A {
virtual int foo (int) const {std::cout << "B::foo() called.n"; return 3;}
virtual void bar () const {std::cout << "B::bar() called.n";}
};
class C : public B {
virtual int foo (int) const {std::cout << "C::foo() called.n"; return 8;}
virtual void bar (char, double) const {std::cout << "C::bar() called.n";}
};
int main() {
A* c = new C;
int n = c->foo(5); // C::foo() called.
c->bar(3, 'c'); // C::bar() called.
n = c->execute<Foo>(5); // C::foo() called.
c->execute<Bar>(3, 'c'); // C::bar() called.
}
但是专用化A::Execute<Foo>
和A::Execute<Bar>
看起来几乎相同,并且理想情况下应该保持非专用化(特别是如果除了foo
和bar
之外还有许多其他虚拟函数(。写的类似于:
template <Enum N>
class A::Execute {
const A* a;
public:
Execute (const A* a_) : a(a_) {}
template <typename... Args>
int operator()(Args&&... args) const {return a->???(std::forward<Args>(args)...);}
};
如何填写???部分理想情况下,我希望使用已经存在的EnumTraits
类。
这是我的尝试。我已经将enum
替换为用作标签的struct
,将EnumTraits
替换为TagTraits
。我更喜欢struct
方法,因为它允许在不影响现有标签的情况下添加新标签。
#include <iostream>
#include <functional>
template <typename T> struct TagTraits;
// Generic implementation of A based on TagTraits.
class A {
template <typename Tag, typename... Args>
class Execute {
const A* a;
public:
Execute (const A* a_) : a(a_) {}
typename TagTraits<Tag>::return_type operator()(Args&&... args) const
{
return (a->*(TagTraits<Tag>::get_funtion_ptr()))(std::forward<Args>(args)...);
}
};
public:
virtual int foo (int) const = 0;
virtual void bar (char, double) const = 0;
template <typename Tag, typename... Args>
typename TagTraits<Tag>::return_type execute(Args&&... args) const
{
return Execute<Tag, Args...>(this)(std::forward<Args>(args)...);
}
};
// tag for foo and the corresponding TagTraits
struct foo_tag {};
template <> struct TagTraits<foo_tag>
{
using return_type = int;
static decltype(&A::foo) get_funtion_ptr(){ return &A::foo;}
};
// tag for bar and the corresponding TagTraits
struct bar_tag {};
template <> struct TagTraits<bar_tag>
{
using return_type = void;
static decltype(&A::bar) get_funtion_ptr(){ return &A::bar;}
};
// Derived classes of A.
class B : public A {
virtual int foo (int) const {std::cout << "B::foo() called.n"; return 3;}
virtual void bar (char, double) const {std::cout << "B::bar() called.n";}
};
class C : public B {
virtual int foo (int) const {std::cout << "C::foo() called.n"; return 8;}
virtual void bar (char, double) const {std::cout << "C::bar() called.n";}
};
// Test B
void test_B()
{
A* aPtr = new B;
int n = aPtr->foo(5); // B::foo() called.
aPtr->bar(3, 'c'); // B::bar() called.
n = aPtr->execute<foo_tag>(5); // B::foo() called.
aPtr->execute<bar_tag>(3, 'c'); // B::bar() called.
}
// Test C
void test_C()
{
A* aPtr = new C;
int n = aPtr->foo(5); // C::foo() called.
aPtr->bar(3, 'c'); // C::bar() called.
n = aPtr->execute<foo_tag>(5); // C::foo() called.
aPtr->execute<bar_tag>(3, 'c'); // C::bar() called.
}
int main()
{
test_B();
test_C();
}
输出:
B::foo() called.
B::bar() called.
B::foo() called.
B::bar() called.
C::foo() called.
C::bar() called.
C::foo() called.
C::bar() called.
不能将继承和多态性与模板元编程结合起来。您要做的是使用编译时机制来调用正确的函数。虽然这是可能的,但您不能取消引用指针来调用正确的函数,并期望它在编译时运行。
从本质上讲,您所做的是在继承层次结构中封装部分模板专门化的层次结构。这有点令人困惑。如果取消引用具有继承层次结构的指针,则调用将通过在虚拟表中查找来解析。
您也可以调用模板编译时调度,但这会有点不同。您需要使用SFINAE来创建一个接口并从那里开始工作
相关文章:
- 在执行其他功能的同时播放动画(LED矩阵和Arduino/ESP8266)
- 多态性和功能结合
- 带内存和隔离功能的SQLite
- 在CMakeLists.txt的安装功能中使用.cmake文件有什么用
- 类模板的成员功能的定义在单独的TU中完全专业化
- 有没有一种方法可以创建一个带有哈希表的数据库,该哈希表具有恒定时间查找功能
- 如何在C++中获得"静态纯虚拟"功能?
- 两个文件使用彼此的功能-如何解决
- 我应该实现右值推送功能吗?我应该使用std::move吗
- QML按钮点击功能执行顺序
- 无法理解此 return 语句的功能,没有它就会发生运行时错误
- 有没有可能有一个只有ADL才能找到的非好友功能
- 功能样式转换从 'int' 到 'ItemType' 的匹配转换
- 文件系统:复制功能的速度秘诀是什么
- 在用于格式4的arm模拟器中实现功能时的一个问题
- 如何在Directwrite中获得给定字体的可用OpenType功能
- 对可变参数使用声明.如何选择正确的功能
- 询问在设计我的手臂模拟器功能表示格式1
- 功能原型的目的
- 将虚拟功能放入家庭