The problem is that you are trying to use a double conversion sequence:
- From lambda function to
std::function<void(Ref*)> - From
std::function<void(Ref*)> to MT
On this path, you need to remove the need for double conversion using
mt["cast via function"] = static_cast<std::function<void(Ref*)>([](Ref*){ /*...*/ }); mt["cast via MT"] = MT([](Ref*){ /*...*/ });
If you want to support conversion from function type to MT , you will need an MT constructor that directly accepts the function type. Assuming none of your other constructors are written using an unlimited template, you can simply add
template <typename Fun> MT::MT(Fun&& fun) : type(ValueType::ccbValue) { this->value.ccb = std::forward<Fun>(fun); }
If you already use the template without restrictions for another type, you will need to use suitable conditions, for example. std::is_convertible<Fun, std::function<void(Ref*)>>::value along with a suitable SFINAE approach to remove the corresponding constructor (s) from the overload set.
source share