Io_obj *istream&作为Stroustrup中函数的指针

问题描述

在C ++编程语言(第四版平装书的650页)中,Stroustrup为映射写了一些代码,其中以字符串为键,函数指针为映射类型(注释是他自己的):

using Pf = Io_obj*(istream&); // pointer to function returning an Io_obj*
std::map<std::string,Pf> io_map;

我对函数指针不是很熟悉,我想编写自己的测试代码,其中有一个字符串,如“ add 1 2”,断开第一个要传递的单词作为映射键,然后将其余的作为映射返回的计算函数指针的参数。我的类型更简单(普通的int和普通的旧字符串),但是花了我一些时间才能将其转换为编译器可以接受的形式。为了简单起见,此处省略了别名:

std::map<std::string,int(std::string)> calc_map; // Stroustrup's syntax (comipile error)...

成为...

std::map<std::string,int(*)(std::string)> calc_map; // syntax that compiles...

问题1:这是clang的一些特殊问题(我在旧Mac笔记本电脑上,因为我的Linux机器已经死了!)还是Stroustrup忘记在示例中添加(*)?我还想念其他东西吗?

此外,他的示例使用别名,在我的简单计算示例中,别名首先转换为:

using Pf = int(std::string); // compiler actually seems to think this is fine
std::map<std::string,Pf> calc_map; // compiler does not like this

当修复后变成...

using Pf = int(*)(std::string); // compiler thinks this is fine too
std::map<std::string,Pf> calc_map; // compiler also likes this and everything works

问题2:编译器不介意将int(std :: string)用作别名是什么东西?

来自clang的可能有用的版本资料:

Apple LLVM version 6.0 (clang-600.0.57) (based on LLVM 3.5svn)
Target: x86_64-apple-darwin13.1.0

谢谢!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...