I am trying to answer this question using SFINAE and decltype. To summarize, the poster wants to use a function that acts differently depending on whether another function is declared in the compilation module (it is declared sooner or later of the corresponding function).
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βs the whole point, I thought, I donβt want the overload to some_function_2_implbe determined because it some_function_1does not exist.
I thought that maybe SFINAE requires the templates to work, so I tried the following (this can help indicate that I do not 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()) {
?