gpt4 book ai didi

c++ - 在我的 void_t 包装器而不是后备上编译时错误

转载 作者:行者123 更新时间:2023-12-05 05:43:49 27 4
gpt4 key购买 nike

最近我尝试编写围绕 void_t 的包装器,如下所示:

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>>;

这里的关键不是将特征应用于多个类,而是在不使用 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 个选项(据我所知)

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

同理,我们可以创建一个“模板类列表”。在此实现中,我们期望类型特征具有 ::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

关于c++ - 在我的 void_t 包装器而不是后备上编译时错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71746557/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com