Search code examples
c++dllautodecltype

Exposing class member functions with deduced types


I'm trying to write an interface class where the user provides their implementation in the form of a DLL. As part of this interface, there are some member functions that return a deduced type determined at runtime e.g:

auto someFunction(int x) -> decltype(auto)

I'm having trouble exposing these functions to be used by the client application. I tried to do some research on this issue and it seems the most popular solution for exporting member functions is to declare them virtual to make use of the vtable. I tried this method but got an error "a virtual function cannot have a deduced return type". Are there any alternatives to the vtable approach?

Thanks.


Solution

  • What you are trying to do is not possible. Virtual functions can't be templates (and you can think of an auto as template in this case). The reason for this is that all type deductions happen at compile time, since there is no type information at run time in C++. And virtual function resolution happens at compile time. The technique (not a silver bullet, but sometimes applicable) you are looking for is called type elision.