专职C++

不能停止的脚步

  C++博客 :: 首页 :: 联系 :: 聚合  :: 管理
  163 Posts :: 7 Stories :: 135 Comments :: 0 Trackbacks

常用链接

留言簿(28)

我参与的团队

搜索

  •  

最新评论

阅读排行榜

评论排行榜

我在看boost的ASIO源代码的时候,发现一个没有见过的模板调用用法.google没有说明,baidu也没有说明,只好自己试一下。下面是ASIO调用的代码:在文件service_registry.hpp中。
 1   // Check whether a service object of the specified type already exists.
 2   template <typename Service>
 3   bool has_service() const
 4   {
 5     boost::asio::detail::mutex::scoped_lock lock(mutex_);
 6 
 7     boost::asio::io_service::service* service = first_service_;
 8     while (service)
 9     {
10       if (service_id_matches(*service, Service::id))
11         return true;
12       service = service->next_;
13     }
14 
15     return false;
16   }
调用的地方,红色标出的地方 
template <typename Service>
bool has_service(io_service& ios)
{
  
// Check that Service meets the necessary type requirements.
  (void)static_cast<io_service::service*>(static_cast<Service*>(0));
  (
void)static_cast<const io_service::id*>(&Service::id);

  
return ios.service_registry_->template has_service<Service>();
}

下面是测试例子,在VS2005中编译通过。原来,函数使用的时候,可以完全指定模板函数定义。
 1 class VV
 
2 {
 
3 public:
 
4     static int getI()
 
5     {
 
6         return 100;
 
7     }
 
8 };
 
9 class V
10 {
11 public:
12     template<class K>
13     void PrintK()
14     {
15         cout<<K::getI()<<endl;
16     }
17 };
18 void m()
19 {
20     V v;
21     v.template PrintK<VV>();
22 }
posted on 2010-10-20 21:32 冬瓜 阅读(4246) 评论(30)  编辑 收藏 引用 所属分类: 原创

Feedback

# re: 分析ASIO原代码,发现一则没见过的模板函数调用 2010-10-21 14:34 空明流转
不加template在gcc上貌似会挂掉。  回复  更多评论
  

# re: 分析ASIO原代码,发现一则没见过的模板函数调用 2010-10-21 14:47 coolypf
这种语法上的问题就不要Google、百度了,
直接看C++标准。

ANSI C++ '03
14.2 Names of template specializations
4 When the name of a member template specialization appears after . or -> in a postfix-expression, or after
nested-name-specifier in a qualified-id, and the postfix-expression or qualified-id explicitly depends on a
template-parameter (14.6.2), the member template name must be prefixed by the keyword template.
Otherwise the name is assumed to name a non-template. [Example:
class X {
public:
template<size_t> X* alloc();
template<size_t> static X* adjust();
};
template<class T> void f(T* p)
{
T* p1 = p->alloc<200>();
// ill-formed: < means less than
T* p2 = p->template alloc<200>();
// OK: < starts template argument list
T::adjust<100>();
// ill-formed: < means less than
T::template adjust<100>();
// OK: < starts template argument list
}
—end example]  回复  更多评论
  

# re: 分析ASIO原代码,发现一则没见过的模板函数调用 2010-10-22 11:22 战魂小筑
也可以这样写v.PrintK<VV>();
就是一种把模板当宏写的方法  回复  更多评论
  

# re: 分析ASIO原代码,发现一则没见过的模板函数调用 2010-10-22 15:46 13
67  回复  更多评论
  

# ><script>< 2010-10-23 21:51 ><script><
><script><  回复  更多评论
  

# re: 分析ASIO原代码,发现一则没见过的模板函数调用 2011-09-13 22:20 test
testing  回复  更多评论
  

# re: 分析ASIO原代码,发现一则没见过的模板函数调用 2011-10-13 14:36 1111111111
11111111111111111 果然不用登陆!  回复  更多评论
  

# re: 分析ASIO原代码,发现一则没见过的模板函数调用 2011-11-13 02:44 ss
想学习  回复  更多评论
  

# re: 分析ASIO原代码,发现一则没见过的模板函数调用 2011-11-24 10:17 ->->
好文章  回复  更多评论
  

# re: 分析ASIO原代码,发现一则没见过的模板函数调用 2012-02-23 10:24 2222222
22222222222222  回复  更多评论
  

# re: 分析ASIO原代码,发现一则没见过的模板函数调用 2012-05-18 21:31 11
牛,拜莫  回复  更多评论
  

# re:asio是什么呀? 2012-05-18 21:32 11
学习一下  回复  更多评论
  

# re: 分析ASIO原代码,发现一则没见过的模板函数调用 2012-05-21 13:18 test
test 学习下啊  回复  更多评论
  

# re: 分析ASIO原代码,发现一则没见过的模板函数调用 2012-07-08 22:22 双方孤独感
许多风格的风格的风格  回复  更多评论
  

# re: 分析ASIO原代码,发现一则没见过的模板函数调用 2012-07-12 11:59 ???
好文  回复  更多评论
  

# re: 分析ASIO原代码,发现一则没见过的模板函数调用 2012-09-06 13:02 ssss
果然是这样的?  回复  更多评论
  

# re: 分析ASIO原代码,发现一则没见过的模板函数调用 2012-09-06 13:03 ssss
果然是没有验证就可以发表评论了  回复  更多评论
  

# re: 分析ASIO原代码,发现一则没见过的模板函数调用[未登录] 2013-04-17 13:33 aa
果然是没有验证就可以发表评论了  回复  更多评论
  

# re: 分析ASIO原代码,发现一则没见过的模板函数调用 2013-06-09 11:49 wooyun
haoba  回复  更多评论
  

# re: 分析ASIO原代码,发现一则没见过的模板函数调用 2013-06-09 15:07 wooyun
<script>alert("test")</script>  回复  更多评论
  

# re: 分析ASIO原代码,发现一则没见过的模板函数调用 2013-07-01 15:18
低级  回复  更多评论
  

# re: 分析ASIO原代码,发现一则没见过的模板函数调用 2013-07-01 15:19
aaaaaa  回复  更多评论
  

# re: 分析ASIO原代码,发现一则没见过的模板函数调用 2013-07-01 15:19
kkkkkkkkkkkkkk  回复  更多评论
  

# re: 分析ASIO原代码,发现一则没见过的模板函数调用 2013-07-11 07:52 z7y
Hacked by z7y!!!  回复  更多评论
  

# re: 分析ASIO原代码,发现一则没见过的模板函数调用 2013-08-14 08:30 xyxyxy
这个BUG一直没修复啊!  回复  更多评论
  

# re: 分析ASIO原代码,发现一则没见过的模板函数调用 2013-08-31 20:12 这是阴谋吗
真的可以耶~  回复  更多评论
  

# # re: 分析ASIO原代码,发现一则没见过的模板函数调用 2013-09-13 20:42 我没登陆这个BUG一直没修复啊!
我没登陆啊 这个BUG一直没修复啊!  回复  更多评论
  

# re: 分析ASIO原代码,发现一则没见过的模板函数调用[未登录] 2016-04-07 10:26 1
111111  回复  更多评论
  

# re: 分析ASIO原代码,发现一则没见过的模板函数调用 2016-04-07 10:30 大坑
大坑大坑  回复  更多评论
  


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理