Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to disable a function if any type of list is passed in

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?

like image 854
Curious Avatar asked Dec 10 '22 14:12

Curious


1 Answers

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&& ) { ... }
like image 91
Barry Avatar answered Mar 07 '23 07:03

Barry