I am trying to create a class holding std::variant
with a member function that would only accept types held by the nested variant object.
That function works basically the same way as variant's operator=
. However, the question is - how do I use std::enable_if
and type_traits
together with template parameter pack?
The example below (attempting to check if any of the Types
is contractible from T
) obviously doesn't compile:
template<typename... Types>
class Wrapper
{
public:
template<typename T, std::enable_if_t<std::is_constructible_v<Types..., T>, bool> = true>
void Set(const T& data) {
m_data = data;
}
private:
std::variant<Types...> m_data;
};
int main()
{
Wrapper<int, float> wrapper;
wrapper.Set(123);
return 0;
}