如果我使用函数指针作为一对的值,我会遇到stl::pair问题2051吗
Am I hitting the stl::pair issue 2051 if I use a function pointer as the value a pair?
我想要一个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;
相关文章:
- 在C++STL中是否有Polyval(Matlab函数)等价物?
- 编译包含字符串的代码时遇到问题
- 为什么这个运算符<重载函数对 STL 算法不可见?
- 在C应用程序中运行C++(带有STL)函数
- 使用2个键的cpp-stl::优先级队列排序不正确
- 遇到新行时,有没有办法停止istream_iterator
- 在STL容器中使用模板类
- 在使用GPU支持编译Tensorflow时,会遇到CUDA_TOOLKIT_PATH未绑定变量
- 如何在C++向量中奇数元素前面加上值-1,我在使用insert函数时遇到了问题
- 在 for 循环中查找问题时遇到困难
- 用C++中的CPerson(类)类型的对象初始化STL矢量
- 将stl字符串缩小到小于15个字符的容量
- 创建结构的数组时遇到分段错误
- 在为LINUX创建共享库时,如何避免STL的私有/弱副本
- 当遇到不是来自 STL 的未捕获异常时,有没有办法打印其信息?
- 使用队列和列表 STL 进行C++分配。在将数据填充到列表或队列中然后打印该数据时遇到问题
- 使用C 中的STL实现DF时遇到的分割故障
- 在朋友函数中遇到STL的麻烦
- 删除元素时使用 STL 映射的迭代器时遇到问题
- 如果我使用函数指针作为一对的值,我会遇到stl::pair问题2051吗