我的 void_t 包装器而不是回退的编译时错误

Compile time error on my void_t wrapper intead of fallback

最近我尝试编写围绕 void_t 的 wrapper 简单的成语如下:

namespace detail {

template <class Traits, class = void>
struct applicable : std::false_type {};

template <class... Traits>
struct applicable<std::tuple<Traits...>, std::void_t<Traits...>>
    : std::true_type {};

}  // namespace detail

template <class... Traits>
using applicable = detail::applicable<Traits...>;

和调用方类似的东西


template <class T>
using call_foo = decltype(std::declval<T>().foo());

template <class T>
using has_foo = applicable<call_foo<T>>;

auto main() -> int {
    std::cout << std::boolalpha << has_foo<std::vector<int>>::value;
}

但是我得到了预期的“false”,我得到了编译时错误:

error: 'class std::vector<int, std::allocator<int> >' has no member named 'foo'
 using has_foo = my::applicable<call_foo<T>>;

怎么了?

更新: 在特征中使用参数包背后的想法是按如下方式使用此适用的元函数:

template <class T>
using call_foo = decltype(std::declval<T>().foo());

template <class T>
using call_boo = decltype(std::declval<T>().boo());

template <class T>
using call_bar = decltype(std::declval<T>().bar());

template <class T>
using has_foo_and_boo_and_bar = applicable<call_foo<T>, call_boo<T>, call_bar<T>>;

这里的关键不是将 trait 应用到多个 class 上,而是将多个 trait 应用到一个 class 上而不使用 std::conjunction.

类似的东西:

#include <type_traits>
#include <vector>
using namespace std;


struct Foo{
    int foo() { return 1 ;}
};


// transform an "maybe type" into a classic type traite
// We use T and not Args... so we can have a default type at the end
// we can use a type container (like tuple) but it need some extra boilerplate
template<template<class...> class Traits, class T, class = void>
struct applicable : std::false_type {};

template<template<class...> class Traits, class T>
struct applicable<
        Traits,
        T, 
        std::void_t< Traits<T> >
    > : std::true_type {};


// not an usual type trait, I will call this a "maybe type"
template <class T>
using call_foo = decltype(std::declval<T>().foo());


// creating a type trait with a maybe type
template<class T>
using has_foo_one = applicable<call_foo, T>;

static_assert( has_foo_one<std::vector<int>>::value == false );
static_assert( has_foo_one<Foo>::value == true  );

// we need a way to check multiple type at once
template <
    template<class...> class Traits,
    class... Args
>
inline constexpr bool all_applicable = (applicable<Traits,Args>::value && ...);

static_assert( all_applicable<call_foo,Foo,Foo> == true  );
static_assert( all_applicable<call_foo,Foo,int> == false  );


template<class ... Args>
struct List{};


// if you want the exact same syntaxe
template<
    template<class...> class Traits, // the type traits
    class List,                      // the extra  boilerplate for transforming args... into a single class
    class = void                     // classic SFINAE
    >                   
struct applicable_variadic : std::false_type {};

template<
    template<class...> class Traits,
    class... Args
    >
struct applicable_variadic 
    <Traits,
    List<Args...>, // can be std::tuple, or std::void_t  but have to match line "using has_foo..."
    std::enable_if_t<all_applicable<Traits, Args...> // will be "void" if all args match Traits
    >
> : std::true_type {};

template<class... Args>
using has_foo = applicable_variadic<call_foo, List<Args...>>;

static_assert( has_foo<Foo,Foo>::value == true  );
static_assert( has_foo<Foo>::value == true  );
static_assert( has_foo<Foo,int>::value == false  );

int main() {
   
    return 1;
 }

https://godbolt.org/z/rzqY7G9ed

你可能可以一次写完,但我把每一部分都分开了。当我稍后回头看我的代码时,我发现它更容易理解。


注:

在您想要的更新中:

template <class T>
using call_foo = decltype(std::declval<T>().foo());

template <class T>
using call_boo = decltype(std::declval<T>().boo());

template <class T>
using call_bar = decltype(std::declval<T>().bar());

template <class T>
using has_foo_and_boo_and_bar = applicable<call_foo<T>, call_boo<T>, call_bar<T>>;

不可能applicable<int, ERROR_TYPE> 不会编译。这不是“替换错误”,而是错误。

你有 2 个选项(据我所知)

  • 使用布尔逻辑applicable<traits_foo<T>::value, traits_bar<T>::value>。注意 value。在这种情况下,每个类型特征都会判断 属性 是否得到尊重,而 applicable 只会检查所有布尔值是否为真。
  • 传递一些模板 class(所以不是 type_traits<T> 而只是 type_traits)和类型来检查和使用适用的 SFINAE。那就是我在下面所做的。

同理,我们可以创建一个“模板列表class”。在此实现中,我们期望类型特征具有 ::value 这就是为什么我通过 has_bar_one 而不是 call_bar

template<template<class...> class... Traits>
struct list_of_template_class{};


template<
    class ListOfTraits,
    class T,                      
    class = void                     
    >                   
struct applicable_X_traits : std::false_type {};

template<
    template<class...> class... Traits ,
    class T
    >
struct applicable_X_traits 
    <list_of_template_class<Traits...>,
    T,
    std::enable_if_t< ( Traits<T>::value && ...) >
> : std::true_type {};


template <class T>
using call_bar = decltype(std::declval<T>().foo());

template<class T>
using has_bar_one = applicable<call_foo, T>;


template<class T>
using has_foo_bar = applicable_X_traits<
                        list_of_template_class<has_bar_one, has_foo_one>,
                        T
                    >;

static_assert(has_foo_bar<Foo>::value == true  );

static_assert(has_foo_bar<int>::value == false  );


struct JustBar {
    void bar() { }
};

static_assert(has_foo_bar<JustBar>::value == false  );

https://godbolt.org/z/K77o3KxTj


或者直接使用Boost::Hana

// If you have an instance of T you can just do :
auto has_foo_bar_simpler = hana::is_valid([](auto&& p) -> std::void_t<
    decltype(p.foo()), 
    decltype(p.bar())
>{ });


static_assert(has_foo_bar_simpler(1) == false  );
static_assert(has_foo_bar_simpler(JustBar{}) == false  );
static_assert(has_foo_bar_simpler(Foo{}) == true  );

// if not 
template<class T>
constexpr bool has_foo_bar_simpler2 = decltype(has_foo_bar_simpler(std::declval<T>())){};
static_assert(has_foo_bar_simpler2<int> == false  );
static_assert(has_foo_bar_simpler2<JustBar> == false  );
static_assert(has_foo_bar_simpler2<Foo> == true  );

https://godbolt.org/z/aM5YT8a56