如果我使用函数指针作为一对的值,我会遇到stl::pair问题2051吗

Am I hitting the stl::pair issue 2051 if I use a function pointer as the value a pair?

本文关键字:stl 遇到 pair 2051 问题 函数 指针 如果      更新时间:2023-10-16

我想要一个std::unordered_map<std::string,MyClass*()>变量。但是,当我尝试实例化它时,我会在内心深处收到一条错误消息,本质上是:

/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 *()]" 

我敢肯定这不是我的错误造成的。我浏览了一下网站,注意到了LWG第2051期的链接使得CCD_ 2的限制性太强。这真的是我看到的吗?如果是,我该怎么做才能解决它?使用一个没有数据的包装类,也许是operator()std::function

如果你想要一个从字符串到MyClass指针的映射,你必须声明:

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

但是,如果您想要映射到函数指针到返回指向MyClass的指针的函数,您可以考虑:

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