如何理解此代码片段中的两对括号? [英] How to understand two pairs of parentheses in this code fragment?

查看:107
本文介绍了如何理解此代码片段中的两对括号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这段代码来自 C ++ primer p.446:

  return hash< string>( )(sd.isbn()); 

我不明白带有两对括号的返回表达式。这本书前面没有类似的语法。

hash< string>()然后<$ $构造一个临时的 std :: hash c $ c>(sd.isbn())调用 operator() 的临时传递 sd.isbn() p>

它与

  std :: hash< std: :字符串>温度; 
return temp(sd.isbn());

有关使用具有运算符()请参阅: C ++ Functors及其用途


This code is from C++ primer p.446:

return hash<string>() (sd.isbn());

I don't understand the return expression with two pairs of parentheses. There's no similar syntax in front of the book.

解决方案

std::hash is a class type. What you are doing here is constructing a temporary std::hash with hash<string>() and then (sd.isbn()) calls the operator() of that temporary passing it sd.isbn().

It would be the same as

std::hash<std::string> temp;
return temp(sd.isbn());

For more reading on using objects that have a operator() see: C++ Functors - and their uses

这篇关于如何理解此代码片段中的两对括号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆