#include <iostream>
using namespace std;
template <size_t N>
typename enable_if<(N > 1), void>::type f(){
cout << N - 1 << ' ';
f<N - 1>();
}
template <size_t N>
typename enable_if<N == 1, void> ::type f() {
cout << 1;
}
int main() {
f<4>();
}
Compiler complains at line 8:
f< N - 1 >();
Call to function
f
that is neither visible in the template definition nor found byADL
.
Reverse the order of your function definitions.
#include <iostream>
#include <type_traits>
using namespace std;
template <size_t N>
typename enable_if<N == 1, void> ::type f() {
cout << 1;
}
template <size_t N>
typename enable_if<(N > 1), void>::type f(){
cout << N - 1 << ' ';
f<N - 1>();
}
int main() {
f<4>();
}
Output:
$ ./a.out
3 2 1 1
Please note that the function is defined below the function call.
You have two possible approaches:
Approach 1:
#include <iostream>
#include <type_traits>
using namespace std;
template <size_t N>
typename enable_if<N == 1, void> ::type f() {
cout << 1;
}
template <size_t N>
typename enable_if<(N > 1), void>::type f(){
cout << N - 1 << ' ';
f<N - 1>();
}
int main() {
f<4>();
}
Approach 2:
You can forward declare the prototype for the N==1
version function.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With