integer_sequence用法的一个问题, 编译错误

C++语言 码拜 8年前 (2016-04-01) 897次浏览
找了一个网上的例子,本人想要用一个模板来处理一个int序列,假如这个序列里面的值都小于某个数,返回true,否则返回false,如下:

#include<utility>
#include<iostream>
using namespace std;
template <std::size_t N, std::size_t... Ix>
bool in_range(std::index_sequence<Ix...>) {
  return ((Ix < N) && ...);
}
int main()
{
    cout<<in_range<10>({1,2,30})<<endl;
    cout<<in_range<10>(1,2,3)<<endl;
    return 0;
}


本人用clang3.8来编译,失败了:

$ clang++ m.cpp -std=c++1z
m.cpp:5:37: error: template argument for template type parameter must be a type
bool in_range(std::integer_sequence<Ix...>) {
                                    ^~~~~
/usr/bin/../lib/gcc/x86_64-linux-gnu/5.3.1/../../../../include/c++/5.3.1/utility:229:21: note:
      template parameter is declared here
  template<typename _Tp, _Tp... _Idx>
                    ^
m.cpp:10:11: error: no matching function for call to "in_range"
    cout<<in_range<10>({1,2,30})<<endl;
          ^~~~~~~~~~~~
m.cpp:11:11: error: no matching function for call to "in_range"
    cout<<in_range<10>(1,2,3)<<endl;
          ^~~~~~~~~~~~
3 errors generated.

本人的代码究竟应该怎么改才对呢,是本人的fold expression用的不对吗?
还请赐教

解决方案

40

传的参数当然应该是 index_sequence<1, 2, 30>{} 这样的

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明integer_sequence用法的一个问题, 编译错误
喜欢 (0)
[1034331897@qq.com]
分享 (0)