Chinaunix

标题: 为什么无法推导模版参数 [打印本页]

作者: math_cai    时间: 2013-10-02 18:25
标题: 为什么无法推导模版参数
  1. template<class char_type,class size_type>
  2. struct hash_LNPS_default
  3. {
  4.         size_type operator()(char_type value)const{
  5.                 return value-char_type('a');
  6.         }
  7. };

  8. template<typename char_type,typename size_type,typename hash_type >
  9. size_type LongestNPS(char_type *str_base,size_type size,hash_type hash=hash_LNPS_default<char_type,size_type>() )
  10. {
  11.         ...
  12. }


  13. size_t max=LongestNPS(str,strlen(str);

复制代码
error C2783: 'size_type LongestNPS(char_type *,size_type,hash_type)' : could not deduce template argument for 'hash_type'
作者: linux_c_py_php    时间: 2013-10-02 22:46
楼主的做法没找到标准。

可以换一种做法如下,但是我查了一下发现C++11才支持函数模板默认参数, C++98只为类支持了这个特性:
  1. template<typename char_type,typename size_type,typename hash_type = hash_LNPS_default<char_type,size_type> >
  2. size_type LongestNPS(char_type *str_base,size_type size,hash_type hash=hash_type() )
  3. {
  4. }
复制代码





欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2