I'm trying to answer this question using SFINAE and decltype. To summarize, the poster wants a function which acts differently depending on whether another function is declared in the compilation unit (be it declared earlier or later than the function in question).
I tried the following:
auto some_function_2_impl(int) -> decltype(some_function_1(), void()) {
cout << "Using some_function_1" << endl;
some_function_1();
}
void some_function_2_impl(long) {
cout << "Not using some_function_1" << endl;
}
void some_function_2() {
return some_function_2_impl(0);
}
However, I get this error message:
main.cpp:4:60: error: 'some_function_1' was not declared in this scope
auto some_function_2_impl(int) -> decltype(some_function_1(), void()) {
That is the whole point, I thought - I don't want that overload of some_function_2_impl
to be defined, because some_function_1
does not exist.
I thought maybe SFINAE requires templates to work, so I tried the following (this may help to indicate that I don't fully know what I'm doing here):
template <int foo>
auto some_function_2_impl(int) -> decltype(some_function_1(), void()) {
cout << "Using some_function_1" << endl;
some_function_1();
}
template <int foo>
void some_function_2_impl(long) {
cout << "Not using some_function_1" << endl;
}
However, now I get the following error:
main.cpp:5:60: error: there are no arguments to 'some_function_1' that
depend on a template parameter, so a declaration of 'some_function_1'
must be available [-fpermissive]
auto some_function_2_impl(int) -> decltype(some_function_1(), void()) {
What am I doing wrong?