I want to create a program that has array with hardcode elements L (e.g.:1,2,3,3), and use template variable arguments to check of the elements is sorted, if not sorted, it will be failed to compile at static_assert, but now the program cannot compile at all:
#include <stdio.h>
#include <vector>
template<int first,int second,int... args>
struct s{
enum{e=first<=second && s<second,args...>::e};
};
template<int first,int second>
struct s{
enum{e=first<=second};
};
#define L 1,2,3,3
//static_assert(s<L>::e!=0,"");
int a[]={L};
int main(){
printf("%d\n",s<L>::e);
return 0;
}
the compile error says:
abc.cpp:5:29: error: too few template arguments for class template 's'
enum{e=first<=second && s<second,args...>::e};
^
abc.cpp:5:29: note: in instantiation of template class 's<3, 3>' requested here
enum{e=first<=second && s<second,args...>::e};
^
abc.cpp:5:29: note: in instantiation of template class 's<2, 3, 3>' requested here
enum{e=first<=second && s<second,args...>::e};
^
abc.cpp:16:19: note: in instantiation of template class 's<1, 2, 3, 3>' requested here
printf("%d\n",s<L>::e);
^
abc.cpp:4:8: note: template is declared here
struct s{
what is the reason? Is it just syntax error in template? or this idea is not possible?if not possible,is there other ways to check if array is sorted at compile time?