Recently, I have been reading source codes of boost::any
, I found that std::any
can work fine in a virtual template member function where templates are forbidden. So, I tried to replace templates with std::any
to implement template<typename T, typename K> auto add(T a, K b) { return a + b; }
just with std::any
.
Sadly, I failed.
Here is what I tried (it can only receive (int a, int b)
and can't receive real any
type, so it is a failure):
auto sum_any(std::any a, std::any b)
{
return std::any_cast<int>(a) + std::any_cast<int>(b);
}
I hope to see your implementations.
Thanks in advance.
Edit:
template<typename T, typename K>
auto sum_any(std::any a, std::any b)
{
return std::any_cast<T>(a) + std::any_cast<K>(b);
}
the codes above con't pass compile:
'auto sum_any(std::any,std::any)': could not deduce template argument for 'T'('K')