岁月流转,往昔空明

C++博客 首页 新随笔 联系 聚合 管理
  118 Posts :: 3 Stories :: 413 Comments :: 0 Trackbacks
我也不知道这个能不能算是2005的bug吧,反正我是想不太明白。
今天在对我的pool使用policy的设计的时候发现的。

//前面代码省略

struct single_thread
{
  template
<class T> struct thread_safe_type
  {
     typedef boost::add_volatile
<T> result;
  }
};

template
<class ThreadingModel>
struct pool : private ThreadingModel
{
  typedef typename ThreadingModel::thread_safe_type
<size_t>::result index_t;//其实有没有typename都一样。
 
//other public members

private:
 index_t first_free_;
};
看起来这段代码是完全能正常工作的,是吧。可是很不幸的是这代码在2005上没法正常通过。
解决的方法很简单:
typedef ThreadingModel TM;
typedef TM::thread_safe_type<size_t>::result index_t;
就可以大功告成了。不知道是不是typename的问题。但是从编译器的出错提示来看并不能和简单的typename的问题混为一谈。另外,如果这个thread_safe_type不是模板而只是一个普通的struct或者typedef的话,也是没有这个问题的。

如果有知道这个问题出现的根本原因的,请指教。

posted on 2008-02-16 21:45 空明流转 阅读(1808) 评论(6)  编辑 收藏 引用

评论

# re: VS 2005模板的一个问题及解决方案 2008-02-17 00:49 eXile
正确的写法,使用template关键字,应该改成这样:

template<class ThreadingModel>
struct pool : private ThreadingModel
{
typedef typename ThreadingModel::template thread_safe_type<size_t>::result index_t;
//other public members

private:
index_t first_free_;
};
  回复  更多评论
  

# re: VS 2005模板的一个问题及解决方案 2008-02-17 11:42 空明流转
呵呵,楼上没跑过吧?这样也是不行的。否则我也不会说这个究竟是bug或者是别的什么。@eXile
  回复  更多评论
  

# re: VS 2005模板的一个问题及解决方案 2008-02-17 13:07 eXile
刚才试了一下, 没有问题, 我用的是VC2005 (SP1)  回复  更多评论
  

# re: VS 2005模板的一个问题及解决方案 2008-02-17 13:08 eXile
一定要有typename, 不然通不过编译....  回复  更多评论
  

# re: VS 2005模板的一个问题及解决方案 2008-02-17 13:32 空明流转
@eXile
typename我倒没丢,丢的是那个template。3Q
  回复  更多评论
  

# re: VS 2005模板的一个问题及解决方案 2008-04-23 11:19 梦在天涯
因为result编译器不知道是static变量还是类型,所以必须要typename来申明是类型!  回复  更多评论
  


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