Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to pass a variable number of arguments of varying types to functions in a C++11 function map?
    primarykey
    data
    text
    <p>I am attempting to learn C++11 and have started writing a program that reads a textfile into a vector of strings, then passes the vector to a function that will ask the user to select the name of the function they wish to apply to the vector. </p> <p>I have a function map like so in C++11:</p> <pre><code>std::map&lt;std::string, void(*)(std::vector&lt;std::string&gt;&amp; v)&gt; funcs { {"f1", f2}, {"f2", f2}, }; </code></pre> <p>And I call it with the following function:</p> <pre><code>void call_function(std::vector&lt;std::string&gt;&amp; v) { std::string func; std::cout &lt;&lt; "Type the name of the function: "; std::cin &gt;&gt; func; std::cout &lt;&lt; "Running function: " &lt;&lt; func &lt;&lt; "\n"; funcs[func](v); } </code></pre> <p>And two example functions would be:</p> <pre><code>void f1(std::vector&lt;std::string&gt;&amp; v) { std::cout &lt;&lt; "lol1"; } void f2(std::vector&lt;std::string&gt;&amp; v) { std::cout &lt;&lt; "lol2"; } </code></pre> <p>As of right now, I am successfully able to pass my vector of strings to the functions by calling them from the function map, however, I want to be able to pass a variable number of arguments of varying types. What I want to be able to do is to change my functions to accept integer and string arguments, but not all of my functions will accept the same amount of arguments or arguments of the same type. </p> <p>For example, I may want to allow one function that is in the map to accept a string and an integer as arguments while another function may only accept a single integer or a single string as the arguments.How can I accomplish this? I've been thus far unable to discover a means of passing variable arguments through map to my functions.</p> <p>Is this possible with std::map? I was also looking into variadic templates in C++11, but I'm not really understanding them that well.</p> <p>Can anyone provide any insight?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload