Jun 2, 2017 at 8:16am UTC
hi gyz. can someone help me, this code get error 2768(illegal use of explicit template arguments) and i dont know why :-??
plz help for function template, i know i can use class/struct to solve it.
tnx
template <int i, int j>
void fn()
{
cout << "1 ";
fn<i + 1, j>();
}
template <int j>
void fn<j, j>()
{
cout << "2 ";
}
int main()
{
fn<0, 4>();
}
Last edited on Jun 2, 2017 at 8:30am UTC
Jun 2, 2017 at 11:01am UTC
@ahura, what are you actually trying to do? (as opposed to what your code looks like).
Did you simply mean
1 2 3 4 5 6 7 8 9 10 11 12 13
#include <iostream>
using namespace std;
template <class T > void fn( T i, T j )
{
cout << i;
if ( i < j ) fn( i + 1, j );
}
int main()
{
fn( 0, 4 );
}
or maybe ...
1 2 3 4 5 6 7 8 9 10 11 12 13
#include <iostream>
using namespace std;
template <int J> void fn( int i )
{
cout << i;
if ( i < J ) fn<J>( i + 1 );
}
int main()
{
fn<4>( 0 );
}
Last edited on Jun 2, 2017 at 11:05am UTC
Jun 2, 2017 at 11:43am UTC
I'm bemused, but this actually worked on both compilers that I tried ...
1 2 3 4 5 6 7 8 9 10 11 12 13
#include <iostream>
using namespace std;
template <int I, int J> void fn()
{
cout << I;
if ( I < J ) fn< I + 1 < J ? I + 1 : J, J>();
}
int main()
{
fn<0,4>();
}
It seemed to be enough to stop the infinite recursion.
Last edited on Jun 2, 2017 at 11:44am UTC