1. <legend id='Uk4nh'><style id='Uk4nh'><dir id='Uk4nh'><q id='Uk4nh'></q></dir></style></legend>

    <i id='Uk4nh'><tr id='Uk4nh'><dt id='Uk4nh'><q id='Uk4nh'><span id='Uk4nh'><b id='Uk4nh'><form id='Uk4nh'><ins id='Uk4nh'></ins><ul id='Uk4nh'></ul><sub id='Uk4nh'></sub></form><legend id='Uk4nh'></legend><bdo id='Uk4nh'><pre id='Uk4nh'><center id='Uk4nh'></center></pre></bdo></b><th id='Uk4nh'></th></span></q></dt></tr></i><div id='Uk4nh'><tfoot id='Uk4nh'></tfoot><dl id='Uk4nh'><fieldset id='Uk4nh'></fieldset></dl></div>
      • <bdo id='Uk4nh'></bdo><ul id='Uk4nh'></ul>
    1. <tfoot id='Uk4nh'></tfoot>

    2. <small id='Uk4nh'></small><noframes id='Uk4nh'>

      使用 C++ 模板实现访问者模式

      时间:2023-05-24
      <tfoot id='MdBGH'></tfoot>
      • <bdo id='MdBGH'></bdo><ul id='MdBGH'></ul>

              <i id='MdBGH'><tr id='MdBGH'><dt id='MdBGH'><q id='MdBGH'><span id='MdBGH'><b id='MdBGH'><form id='MdBGH'><ins id='MdBGH'></ins><ul id='MdBGH'></ul><sub id='MdBGH'></sub></form><legend id='MdBGH'></legend><bdo id='MdBGH'><pre id='MdBGH'><center id='MdBGH'></center></pre></bdo></b><th id='MdBGH'></th></span></q></dt></tr></i><div id='MdBGH'><tfoot id='MdBGH'></tfoot><dl id='MdBGH'><fieldset id='MdBGH'></fieldset></dl></div>

                <legend id='MdBGH'><style id='MdBGH'><dir id='MdBGH'><q id='MdBGH'></q></dir></style></legend>
                  <tbody id='MdBGH'></tbody>

                <small id='MdBGH'></small><noframes id='MdBGH'>

                本文介绍了使用 C++ 模板实现访问者模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                限时送ChatGPT账号..

                我一直在尝试通过使用 C++ 模板来实现访问者模式来减少代码中样板的数量.到目前为止,我想出了这个:

                I've been trying to reduce the amount of boilerplate in my code, by using C++ Templates to implement the visitor pattern. So far I've come up with this:

                class BaseVisitor {
                public:
                    virtual ~BaseVisitor() {}
                };
                
                template<typename T>
                class Visitor : public BaseVisitor {
                public:
                    virtual void visit(T& /* visitable */) = 0;
                };
                
                template<typename Derived>
                class Visitable {
                public:
                    void accept(Visitor<Derived>& visitor) {
                        visitor.visit(static_cast<Derived&>(*this));
                    }
                };
                

                Visible 的每个子类如下所示:

                And each subclass of Visitable looks like this:

                class Mesh : public Object, public Visitable<Mesh> {};
                class Text : public Object, public Visitable<Text> {};
                

                最后访问者看起来像这样:

                And finally the Visitor looks like this:

                class Renderer : public Visitor<Mesh>, public Visitor<Text> {}
                

                到目前为止一切顺利……现在问题来了:

                So far so good... now here's the problem:

                for(Scene::iterator it = scene.begin(); it != scene.end(); ++it) {
                    Object& object = static_cast<Object&>(*it);
                    if(pre_visit(object)) {
                        object.accept(this); ///Erm, what do I cast to??
                        post_visit(object);
                    }
                }
                

                我需要以某种方式强制转换为 Visitable 以便我可以调用 accept(),但显然我不知道 T 是什么.或者,我无法向 Visitable 模板添加虚拟 accept(),因为我不知道它应该采用什么参数.

                I need to somehow cast to Visitable so that I can call accept(), but obviously I don't know what T is. Alternatively I can't add a virtual accept() to the Visitable template, because I don't know what argument it should take.

                任何 C++ 模板大师都知道如何使这个工作?

                Any C++ Templating guru's out there know how to make this work?

                推荐答案

                这可以在 C++11 中使用可变参数模板来完成.继续皮特的回答:

                This can be done in C++11 using variadic templates. Continuing from Pete's answer:

                // Visitor template declaration
                template<typename... Types>
                class Visitor;
                
                // specialization for single type    
                template<typename T>
                class Visitor<T> {
                public:
                    virtual void visit(T & visitable) = 0;
                };
                
                // specialization for multiple types
                template<typename T, typename... Types>
                class Visitor<T, Types...> : public Visitor<Types...> {
                public:
                    // promote the function(s) from the base class
                    using Visitor<Types...>::visit;
                
                    virtual void visit(T & visitable) = 0;
                };
                
                template<typename... Types>
                class Visitable {
                public:
                    virtual void accept(Visitor<Types...>& visitor) = 0;
                };
                
                template<typename Derived, typename... Types>
                class VisitableImpl : public Visitable<Types...> {
                public:
                    virtual void accept(Visitor<Types...>& visitor) {
                        visitor.visit(static_cast<Derived&>(*this));
                    }
                };
                

                Visitable 的子类:

                class Mesh : public Object, public VisitableImpl<Mesh, Mesh, Text> {};
                class Text : public Object, public VisitableImpl<Text, Mesh, Text> {};
                

                一个 Visitor 子类:

                class Renderer : public Visitor<Mesh, Text> {};
                

                不清楚你的 Scene 容器的 value_type 是什么,但你需要获得一个引用或指向 Visitable 调用accept:

                It's not clear what the value_type of your Scene container is but you need to obtain a reference or pointer to Visitable<Mesh, Text> on which to call accept:

                for(Scene::iterator it = scene.begin(); it != scene.end(); ++it) {
                    Visitable<Mesh, Text>& object = static_cast<Visitable<Mesh, Text>&>(*it);
                    if(pre_visit(object)) {
                        object.accept(*this);
                        post_visit(object);
                    }
                }
                

                这篇关于使用 C++ 模板实现访问者模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:解密 C++ 模板错误消息 下一篇:C++在编译时将整数转换为字符串

                相关文章

                最新文章

                <small id='qrX5Y'></small><noframes id='qrX5Y'>

              • <legend id='qrX5Y'><style id='qrX5Y'><dir id='qrX5Y'><q id='qrX5Y'></q></dir></style></legend>

                    <bdo id='qrX5Y'></bdo><ul id='qrX5Y'></ul>

                    <tfoot id='qrX5Y'></tfoot>
                  1. <i id='qrX5Y'><tr id='qrX5Y'><dt id='qrX5Y'><q id='qrX5Y'><span id='qrX5Y'><b id='qrX5Y'><form id='qrX5Y'><ins id='qrX5Y'></ins><ul id='qrX5Y'></ul><sub id='qrX5Y'></sub></form><legend id='qrX5Y'></legend><bdo id='qrX5Y'><pre id='qrX5Y'><center id='qrX5Y'></center></pre></bdo></b><th id='qrX5Y'></th></span></q></dt></tr></i><div id='qrX5Y'><tfoot id='qrX5Y'></tfoot><dl id='qrX5Y'><fieldset id='qrX5Y'></fieldset></dl></div>