Hello,
I implemented the follolwing function:
template< int n >
struct GetNRands{
static void fillVector( set<int>& v, int max ){
if( n > 0 ){
int f;
int pos;
do{
f = rand();
pos = f * max / RAND_MAX;
}
while( v.find( f ) != v.end());
v.insert( pos );
GetNRands< n - 1 >::fillVector( v, max );
}
return;
}
};
... but the compiler gets stuck and doesn't finish the build (VC++ compiler).
This can be easily solved by removing the if condition and specializing the template for the case n = 0:
template< int n >
struct GetNRands{
static void fillVector( set<int>& v, int max ){
//if( n > 0 ){
int f;
int pos;
do{
f = rand();
pos = f * max / RAND_MAX;
}
while( v.find( f ) != v.end());
v.insert( pos );
GetNRands< n - 1 >::fillVector( v, max );
//}
return;
}
};
template<>
struct GetNRands<0>{
static void fillVector( set<int>& v, int max ){}
};
Can someone explain to me why doesn't the first version compile?
Greetings,
Rui