gpt4 book ai didi

c++ - N4502 提案如何运作? ( "detection idiom")

转载 作者:太空狗 更新时间:2023-10-29 23:12:41 28 4
gpt4 key购买 nike

<分区>

我在看提案N4502 ,并一直在努力解决这个问题。直到第 5 节我都还不错,但后来我认为我理解的东西就消失了。也许这就是我的看法。鉴于以下情况:

// primary template handles all types not supporting the operation:
template< class, template<class> class, class = void_t< > >
struct
detect : false_type { };
// specialization recognizes/validates only types supporting the archetype:
template< class T, template<class> class Op >
struct
detect< T, Op, void_t<Op<T>> > : true_type { };

To use this detect metafunction, we supply it with another metafunction (i.e., a meta-callback) that fills the role of the archetypal expression. For example, here is an implementation of the is_assignable type trait:

// archetypal expression for assignment operation:
template< class T >
using
assign_t = decltype( declval<T&>() = declval<T const &>() )
// trait corresponding to that archetype:
template< class T >
using
is_assignable = detect<void, assign_t, T>;

我应该能够检查类型是否可分配。没有给出如何使用它的例子,所以我假设它应该像这样简单:

static_assert(is_assignable<int>::value, "Not assignable.");

现在光看这个,好像不太对。我看不到 assign_t 会以任何方式与 T 类型进行交互。

这对我来说是这样的:

is_assignable<int>-> detect<void, assign_t, int>

然后它将无法匹配任何特化并转到继承自 std::false_type 的基本情况。

编译此 here似乎同意我的理解。

那么,我错过了什么?这应该如何使用?

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