1

I want to have a std::unordered_map<std::string,MyClass*()> variable. But - when I try instantiating that, I get an error message, deep inside, which is essentially:

/usr/include/c++/4.9/bits/stl_pair.h(102): error: a function type is not allowed here
          detected during:
            instantiation of class "std::pair<_T1, _T2> [with _T1=const std::string, _T2=MyClass *()]" 

I'm pretty sure this isn't due to a bug of mine. I've browsed around the site and noticed a link to LWG issue 2051 making std::pair too restrictive. Is that really what I'm seeing? If so, what should I do to work around it? Use a wrapper class no data and operator() maybe? std::function?

T.C.
  • 133,968
  • 17
  • 288
  • 421
einpoklum
  • 118,144
  • 57
  • 340
  • 684
  • 1
    To be clear, you want to map from string to pointer-to-function-taking-no-arguments-and-returning-pointer-to-MyClass ? If so, then change the second argument to `MyClass *(*)()` – M.M Dec 21 '15 at 23:45
  • 1
    I don't see how issue 2051 is related – M.M Dec 21 '15 at 23:46
  • I don't think a container of functions is legal - [container.requirements.general] says that contains store *objects*. So to implement a container of functions you actually have to have a container of function pointers (or a class that wraps a function pointer, like `std::function`, or functor objects). – M.M Dec 21 '15 at 23:52
  • @M.M: FIxed that. Now I'll go look if that problem isn't actually in my code somewhere. – einpoklum Dec 21 '15 at 23:53
  • 1
    your error message doesn't correspond to your code... `_T2` would not be `MyClass *()` in the updated code. If you still have trouble then post a MCVE. [Working example](http://goo.gl/EoiHqS) – M.M Dec 21 '15 at 23:53

1 Answers1

4

If you want to have a map from a string to a MyClass pointer, you have to declare:

 std::unordered_map<std::string,MyClass*> x;

But if you want to have a map to a function pointer to a function returning a pointer to MyClass you could consider:

std::unordered_map<std::string,MyClass*(*)()> x;
Christophe
  • 68,716
  • 7
  • 72
  • 138
  • WIll check to see if that was the issue or if I just misquoted the definition (there's some `using = ` involved which I've removed). – einpoklum Dec 21 '15 at 23:54