<tfoot id='i8HU5'></tfoot>

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

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

      如何在容器中存储具有不同签名的功能对象?

      How to store functional objects with different signatures in a container?(如何在容器中存储具有不同签名的功能对象?)
          <legend id='XwLvG'><style id='XwLvG'><dir id='XwLvG'><q id='XwLvG'></q></dir></style></legend>

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

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

              <tfoot id='XwLvG'></tfoot>

                  <tbody id='XwLvG'></tbody>
                本文介绍了如何在容器中存储具有不同签名的功能对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                假设我们有两个函数 (void : ( void ) )(std::string : (int, std::string)) 并且我们可以有还有 10 个.所有(或其中一些)接受不同的参数类型,并且可以返回不同的类型.我们想将它们存储在 std::map 中,所以我们得到了这样的 API:

                So imagine we had 2 functions (void : ( void ) ) and (std::string : (int, std::string)) and we could have 10 more. All (or some of them) take in different argument types and can return different types. We want to store them in a std::map, so we get an API like this:

                //Having a functions like:
                int hello_world(std::string name, const int & number )
                {
                    name += "!";
                    std::cout << "Hello, " << name << std::endl;
                    return number;
                }
                //and
                void i_do_shadowed_stuff()
                {
                    return;
                }
                
                //We want to be capable to create a map (or some type with similar API) that would hold our functional objects. like so:
                myMap.insert(std::pair<std::string, fun_object>("my_method_hello", hello_world) )
                myMap.insert(std::pair<std::string, fun_object>("my_void_method", i_do_shadowed_stuff) )
                //And we could call tham with params if needed:
                int a = myMap["my_method_hello"]("Tim", 25);
                myMap["my_void_method"];
                

                我想知道如何将许多不同的功能放入同一个容器中.具体来说,如何使用 Boost 在 C++03 中做到这一点.

                I wonder how to put many different functions into the same container. Specifically, how to do this in C++03 using Boost.

                API 应该独立于实际的函数类型(具有 int a = myMap["my_method_hello"]("Tim", 25); 而不是 int a = myMap["my_method_hello"]("Tim", 25);).

                The API should be independent from the actual function types (having int a = myMap["my_method_hello"]("Tim", 25); not int a = myMap<int, (std::string, int)>["my_method_hello"]("Tim", 25);).

                推荐答案

                #include <functional>
                #include <iostream>
                #include <string>
                #include <map>
                
                class api {
                    // maps containing the different function pointers
                    typedef void(*voidfuncptr)();
                    typedef int(*stringcrintptr)(std::string, const int&);
                
                    std::map<std::string, voidfuncptr> voida;
                    std::map<std::string, stringcrintptr> stringcrint;
                public:
                    // api temp class
                    // given an api and a name, it converts to a function pointer
                    // depending on parameters used
                    class apitemp {
                        const std::string n;
                        const api* p;
                    public:
                        apitemp(const std::string& name, const api* parent)
                            : n(name), p(parent) {}
                        operator voidfuncptr()
                        { return p->voida.find(n)->second; }
                        operator stringcrintptr()
                        { return p->stringcrint.find(n)->second; }
                    };
                
                    // insertion of new functions into appropriate maps
                    void insert(const std::string& name, voidfuncptr ptr)
                    { voida[name]=ptr; }
                    void insert(const std::string& name, stringcrintptr ptr)
                    { stringcrint[name]=ptr; }
                    // operator[] for the name gets halfway to the right function
                    apitemp operator[](std::string n) const
                    { return apitemp(n, this); }
                };
                

                用法:

                api myMap; 
                
                int hello_world(std::string name, const int & number )
                {
                    name += "!";
                    std::cout << "Hello, " << name << std::endl;
                    return number;
                }
                
                int main()
                {
                    myMap.insert("my_method_hello", &hello_world );
                    int a = myMap["my_method_hello"]("Tim", 25);
                }
                

                不是很漂亮.更好的建议是,即使是远程操作,也不要像您正在尝试做的那样做任何事情.

                Not very pretty. Better advice is to not do anything even remotely like whatever it is you're trying to do.

                请注意,这要求所有具有相同参数的函数返回相同的类型.

                Note that this requires all functions with the same parameters to return the same type.

                这篇关于如何在容器中存储具有不同签名的功能对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                How to print vector#39;s data(如何打印矢量的数据)
                Visual C++ appends 0xCC (int3) bytes at the end of functions(Visual C++ 在函数末尾附加 0xCC (int3) 字节)
                How to use a variable inside a _T wrapper?(如何在 _T 包装器中使用变量?)
                MSVC++ warning flags(MSVC++ 警告标志)
                How to read file which contains uxxxx in vc++(如何在vc++中读取包含uxxxx的文件)
                stack overflow error in C++ program(C++程序中的堆栈溢出错误)

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

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

                        • <tfoot id='XLAav'></tfoot>