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

I am trying to disable a function if any type of list class is passed to 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 an 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 has a typedef member value_typeto get the value of the thing stored in it.

Any idea how I can fix this?

+4
source share
2 answers

The direct problem is here:

std::list<typename ContainerType::value_type>>::value>>
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

In your example, ContainerTypeis a reference type ( std::vector<int>&), and you cannot access typedefs from a reference type. First you need to remove the link.

But we can make it easier by simply ignoring the part KeyType:

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&& ) { ... }
+8

SO post. :

#include <type_traits>

template<template<typename...> class TT, typename T>
struct is_instantiation_of : std::false_type { };

template<template<typename...> class TT, typename... Ts>
struct is_instantiation_of<TT, TT<Ts...>> : std::true_type { };

template <typename ContainerType, typename KeyType,
          typename = std::enable_if_t<!is_instantiation_of<
            std::list, std::decay_t<ContainerType>>::value>>
void func(ContainerType&& container, KeyType&& key)

, faffing around std::list .

+5

Source: https://habr.com/ru/post/1658616/


All Articles