- 論壇徽章:
- 2
|
找了一個網(wǎng)上的例子,我想要用一個模板來處理一個int序列,如果這個序列里面的值都小于某個數(shù),返回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;
- }
復(fù)制代碼 ----------------------------
我用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.
復(fù)制代碼 我的代碼究竟應(yīng)該怎么改才對呢,是我的fold expression用的不對嗎?
還請賜教 |
|