c – 使用PIMPL习语时有没有办法限制重复的样板?

c – 使用PIMPL习语时有没有办法限制重复的样板?,第1张

概述我有类似以下内容: // foo.h:class foo {public: foo(); ~foo(); // note: the param type repetition here is only incidental, assume the // functions can't easily be made to share type signature 我有类似以下内容:
// foo.h:class foo {public:    foo();     ~foo();    // note: the param type repetition here is only incIDental,assume the    // functions can't easily be made to share type signatures    voID bar(a b,c d);                   voID baz(a b,c d,e f,g h,i j);    voID quux(a b,i j);private:    class impl;    impl *m_pimpl;}

然后:

// foo.cpp:class foo::impl {public:    voID bar(a b,c d);    voID baz(a b,i j);private:    // lots of private state,helper functions,etc. };voID foo::impl::bar(a b,c d) { ... }voID foo::impl::baz(a b,i j) { ... }voID foo::impl::quux(a b,i j) { ... }foo::foo() : m_pimpl(new impl()) { }foo::~foo() { delete m_pimpl; m_pimpl = NulL; }voID foo::bar(a b,c d) {    return m_pimpl->bar(b,d);}voID foo::baz(a b,i j) {    return m_pimpl->baz(b,d,f,h,j)}voID foo::quux(a b,i j) {    return m_pimpl->quux(b,j);}

这里有很多重复的bar,baz和quux:

>一旦进入foo的声明
>一旦进入foo :: impl的声明
>一旦进入foo :: impl的定义
> foo定义中的两次:一次用于foo的函数参数列表,再次调用相应的foo :: impl函数.

对于除了最后一个之外的每一个我必须写出整个参数列表,其类型可以更多地涉及.

如果有的话,减少重复的最佳方法是什么?一个简单的方法是内联foo :: impl公共函数的定义,但是除了设计类以外的任何东西以外还有更少的公共函数吗?

解决方法 使用签名,我们可以将其减少到3个提及,加上1组前锋:
using signature_1 = int(int);struct foo {  signature_1 x;  foo();  ~foo();private:  struct fooimpl;  std::unique_ptr<fooimpl> pimpl;};int main() {  foo f;  std::cout << f.x(1) << '\n';}struct foo::fooimpl {  signature_1 x;  int v = 3;};foo::~foo() = default;foo::foo():pimpl(new foo::fooimpl()){}int foo::x(int y){return pimpl->x(y);}int foo::fooimpl::x(int y){return y+v;}

如果我们的pimpl是纯虚拟类,我们可以将它降低到2.从decltype(& foo :: method) – >签名写一个映射,并让纯虚拟接口使用该(和decltype)来创建纯虚拟类的签名.

在foo中写入一次签名,decltype-在foo_impl_interface中确定它,然后在cpp文件中的foo_impl内实现它.再加上一组前锋.

template<class MethodPtrType> struct method_sig; template<class MethodPtrType> using method_sig_t = typename method_sig<MethodPtrType>::type; template<class T,class R,class...Args> struct method_sig< R(T::*)(Args...) > {   using type=R(Args...); };

加上另外11个奇数专长(叹气)来获得所有案例. (据我所知,const& const const&& const volatile const volatile& const volatile&&&&& volatile volatile&&&& qualifIErs都是必需的).

现在method_sig_t< decltype(& foo :: x)>是int(int),我们可以使用:

struct foo_impl_interface {  virtual method_sig_t<decltype(&foo::x)> x = 0;  virtual ~foo_impl_interface() {}};

假设我们使用pimpl来规范我们的类型而不是隐藏状态.

最后,不是在pimpl中实现大部分代码,而只是在pimpl中存储STATE,将代码保留在类本身中.

这给你“其他人不依赖于我的大小”:谁关心代码是在foo还是foo_impl中读取foo_impl的状态?那么如果你没有做foo_impl_interface技术,为什么要转发呢?

总结

以上是内存溢出为你收集整理的c – 使用PIMPL习语时有没有办法限制重复的样板?全部内容,希望文章能够帮你解决c – 使用PIMPL习语时有没有办法限制重复的样板?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1239379.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-06
下一篇 2022-06-06

发表评论

登录后才能评论

评论列表(0条)

保存