I am trying to disable a function if any type of a list class is passed into the function with the following enable_if
template <typename ContainerType, typename KeyType,
typename = std::enable_if_t<!std::is_same<
std::decay_t<ContainerType>,
std::list<typename ContainerType::value_type>>::value>>
void func(ContainerType&& container, KeyType&& key)
But when I call func with vector<int>
I get the error
candidate template ignored: substitution failure [with ContainerType = std::__1::vector<int, std::__1::allocator<int> > &, KeyType = int]: type 'std::__1::vector<int, std::__1::allocator<int> > &' cannot be used prior to '::' because it has no
members
A vector does have the member typedef value_type
to get the value of the thing stored in it..
Any idea how I can go about fixing this?
The direct problem is here:
std::list<typename ContainerType::value_type>>::value>>
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
In your example, ContainerType
is a reference type (std::vector<int>&
) and you can't access typedefs off of a reference type. You'd have to remove the reference first.
But we can do it simpler by just ignoring the KeyType
part:
template <class X> struct is_list : std::false_type { };
template <class T, class A> struct is_list<std::list<T,A>> : std::true_type { };
template <class Container, class Key,
std::enable_if_t<!is_list<std::decay_t<Container>>::value>* = nullptr>
void func(ContainerType&&, Key&& ) { ... }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With