Search code examples
c++c++11variadic-functionsstd-function

C++ pass parameter pack to std::map results in error C3245


I try to call functions hold in a map (to achieve reflection), with passed arguments as parameter pack which might look a bit strange. I want to get it to run anyway. Currently I end up with the following error:

main.cpp(36): error C3245: 'funcMapA': use of a variable template requires template argument list
main.cpp(23): note: see declaration of 'funcMapA'

Here is my minimum (not)working example:

#include <functional>
#include <map>
#include <string>
#include <sstream>
#include <iostream>
#include <iterator>
#include <vector>
#include <utility>

void DoStuff_1(int i) {
  std::cout << "DoStuff_1 " << i << "\n";
}

void DoStuff_2(int i, int k) {
  std::cout << "DoStuff_2 " << i << ", " << k << "\n";
}

void DoStuff_3(int i, int k, int l) {
  std::cout << "DoStuff_3 " << i << ", " << k << ", " << l << "\n";
}

template <typename ... Ts>
std::map<std::string, std::function<void(Ts&& ... args)>> funcMapA = {
  {"DoStuff_1", [](Ts&& ... args) {DoStuff_1(std::forward<Ts>(args)...); }},
  {"DoStuff_2", [](Ts&& ... args) {DoStuff_2(std::forward<Ts>(args)...); }},
  {"DoStuff_3", [](Ts&& ... args) {DoStuff_3(std::forward<Ts>(args)...); }}
};

std::map<std::string, std::function<void(int, int, int)>> funcMapB = {
  {"DoStuff_1", [](int x, int y, int z) {DoStuff_1(x); }},
  {"DoStuff_2", [](int x, int y, int z) {DoStuff_2(x, y); }},
  {"DoStuff_3", [](int x, int y, int z) {DoStuff_3(x, y, z); }}
};

int main(int argc, char** argv) {
  funcMapA["DoStuff_" + std::to_string(3)](1, 2, 3); //Failing
  funcMapB["DoStuff_" + std::to_string(3)](1, 2, 3); //Working
  getchar();
  return 0;
}

How can I get this (funcMapA) to work?


Solution

  • There are 2 problems:

    First: You have to provide the template arguments like this:

    funcMapA<int,int,int>["DoStuff_" + std::to_string(3)](1, 2, 3);
    

    Second: But if you do this, your template implementation will fail, because:

    {"DoStuff_1", [](Ts&& ... args) {DoStuff_1(std::forward<Ts>(args)...); }},
    {"DoStuff_2", [](Ts&& ... args) {DoStuff_2(std::forward<Ts>(args)...); }},
    {"DoStuff_3", [](Ts&& ... args) {DoStuff_3(std::forward<Ts>(args)...); }}
    

    you forward 3 arguments to DoStuff1 and DoStuff2 which is not what you want.