A tiny switcher for arbitrary types in C++11 I was about to use the awful enum+map trick to switch on std::strings when I thought to see if I could let some variadic templates do the job for me and I started to play...

#include &lt;string&gt;
#include &lt;iostream&gt;
#include &lt;functional&gt;
#include &lt;unordered_map&gt;

template&lt;typename Key = std::string, typename Fun = std::function&lt;void(void)&gt; &gt;
class Switcher {
  std::unordered_map&lt;Key,Fun&gt; map;
  Fun fdefault;
 public:
  template&lt;typename... O&gt;
  Switcher(const Fun&amp; f, const Key&amp; k, const Fun&amp; f2, const O&amp;... o): 
    Switcher(f2, o...) { map.emplace(k,f); }
  template&lt;typename... O&gt;
  Switcher(const Fun&amp; f, const Key&amp; k, const Key&amp; k2, const O&amp;... o):
    Switcher(f, k2, o...) { map.emplace(k,f); }

  Switcher(const Fun &amp; f, const Key &amp; k) { map.emplace(k,f); }
  Switcher(const Fun &amp; f) { fdefault=f; }

  Switcher &amp; operator()(const Key &amp; k) {
    if (map.count(k)) map.at(k)();
    else if (fdefault) fdefault();
    return *this;
  }
};

int main() {
  std::string s1("hi");
  std::string s2("dear");
  Switcher&lt;&gt; switcher ( //whatever std::function here , all the keys here
                        [](){std::cout &lt;&lt; "Hello ";  }, "hi",
                        [](){std::cout &lt;&lt; "World ";  }, "dear", "buddy",
                        [](){std::cout &lt;&lt; "Default ";}
                      );
  switcher(s1);
  switcher(s2);
  switcher("hi")("buddy")("?");
  return 0;
}


Of course is not the same as a switch statement, but it looks handy and for simple purposes it goes quite close, doesn't it?
While the idea is OK, this seems to add very little over a (unordered_)map of strings to functions (created directly). The client code is almost the same as your client code anyway:

int main() {
    typedef std::pair&lt;std::string, std::function&lt;void(void)&gt;&gt; pair;
    std::string s1("hi");
    std::string s2("dear");

    auto defaultFn = [](){ std::cout &lt;&lt; "Default "; };

    std::map&lt;std::string, std::function&lt;void(void)&gt;&gt; switcher{
        pair{"hi", [](){ std::cout &lt;&lt; "Hello "; } },
        pair{"dear", [](){ std::cout &lt;&lt; "World "; } },
    }

    switcher[s1]();
    switcher[s2]();

    switcher["hi"](); // not supported: chaining calls ("buddy")("?");

    if(!switcher.count("buddy"))
        defaultFn();
    else
        switcher["buddy"]();
    return 0;
}


The code is a bit bigger, and some syntactic sugar is missing (stuff that you added in your example, like chaining calls and multiple keys per entry) but the code is semantically the same.

My point to adding this code: your class only makes sense if you use lots of switches (on strings) in your code, and usually in OOP, if you do have big switches in your code, you should extract them as a hierarchy of classes (or a table of functions) anyway.