I suppose the basic premise of this question is that I'm trying to use enable_if
along with Argument Dependent Lookup (ADL), but I'm not sure if it's possible. I do see on this page that
Template argument deduction takes place after the function template name lookup (which may involve argument-dependent lookup) and before template argument substitution (which may involve SFINAE) and overload resolution.
So I imagine this won't work, but in the spirit of learning I'd like to put the question out there.
Here's an example of what I'm trying to get to happen:
#include <iostream>
namespace lib1 {
template <typename T>
void archive(T & t)
{
serialize(t);
}
}
namespace lib2 {
struct VectorInt {
int x;
int y;
};
struct VectorDouble {
double x;
double y;
};
template<typename T>
void serialize(std::enable_if<std::is_same<T, VectorInt>::value, T>::type & vect) {
std::cout << vect.x << std::endl;
}
// maybe do something different with VectorDouble. Overloading would work,
// but I'm curious if it can be made to work with enable_if
}
int main() {
lib2::VectorInt myvect;
myvect.x = 2;
lib1::archive(myvect);
}
The example is loosely based on something I'm trying to do with the cereal library. In my case I have several different types of vectors and matrices, and while I can use overloading to get functions to resolve properly, I was curious to use the enable_if
feature to see if I could shorten the code.
Anyway, trying to compile that gives a message "error: variable or field 'serialize' declared void".
It's my understanding that this won't work because the enable_if
is evaluated only after argument dependent lookup? Is that right?
For those that want to play around with this, I have the code up on repl.it: https://repl.it/repls/HalfBlandJumpthreading