How to choose a class optimally at compile time aka class-overload-set?
I know that there is a way to achieve the following code where the compiler chooses the best fit function for the arguments using overloaded lambdas
auto foo_wrapper = FunctionWrapper([](int a) { return a+1;},
[](double b) { return b/2; });
assert(foo_wrapper(3.0) - foo_wrapper(0) == 0.5);
The links to read more about this trick are at the bottom of this question
However, is there a trick to let the compiler choose the class of an object from a list of classes based on the functions called?
class Foo { v(int); };
class Bar { p(int); v(int); };
auto foobar_wrapper = ClassWrapper(Foo, Bar);
// some syntax trick here to achieve the equivalent of the following
// Some class to take them as input by comma separated values would also be ok
{
foobar_wrapper x;
x.v();
static_assert(std::is_same_v<decltype(x), Foo>);
}
{
foobar_wrapper y;
y.p();
y.v();
static_assert(std::is_same_v<decltype(x), Bar>);
}
// No need to expose the internal x and y variables
// Can't access x and y here
This will essentially allow me to choose a class without mutex if none of the functions to lock are called.
Given that I don't care about the internal layout of the variable (ie I'm not sprinkling memcpy), what options are available for me to let the compiler decide the best class from a ranked list of candidates?
Is something like this even possible without explicit help from compiler (like syntax such as exceptions, coroutines, etc.)? Would any of the introspection features (metaclass and others) being discussed help in achieving this?
Further reading (for some reason, inlining the links wasn't working)
- https://cpptruths.blogspot.com/2014/05/fun-with-lambdas-c14-style-part-2.html
- https://martinecker.com/martincodes/lambda-expression-overloading/
- https://stackoverflow.com/a/32476942
EDIT:
Found the answer. It's complicated. But, for details, please watch https://www.youtube.com/watch?v=WBTNCYT20f0
Posted Sep 2, 2019, 12:43 PM UTC · edited
Nothing I'm aware of, and I don't know of any proposals that would help. Are there any languages that do this? I'd be interested. Seems like it might useful.
I can't think of any way to get even close to what you're asking, really. In C++, files compile sequentially and types are chosen when the compiler reaches the code without looking any further. The two main exceptions to that that I'm aware of are template SFINAE and function auto return types, and I can't think how to use either of them to help here.
Languages with a 2 step compilation (JIT/interpreter) or frameworks built for this purpose often do this. In the first pass they log all functions used and build a call-graph and when the evaluate function is called, they create an object of the required type and execute the call graph. This is usually not elegant or generic code.
Tensorflow had a flow like this. AFAIK almost all JS engines like JavaScript Core, SpiderMonkey and V8 claim to perform such features like choosing the optimal object layouts (functions, classes, null: all are objects) based on runtime usage statistics (not exactly compile time) in their multi-tiered JIT/interpreter loops
Thanks for the HUGE hint. I've put together a maybe correct prototype of what's required. The following code will not compile, but should show what's on my mind right now
Right now, the issue is the overload class which needs to behave more like an overload_list instead of the overload_set behavior I've linked to.
EDIT: It's a wrap. Found the video. It describes almost everything required. https://www.youtube.com/watch?v=WBTNCYT20f0