69

我正在观看Walter Brown 关于模板元编程的 CppCon2014 演讲的第二部分,在此期间他讨论了他的新颖void_t<>构造的用途。在他的演讲中,Peter Sommerlad 问了他一个我不太明白的问题。(链接直接指向问题,讨论中的代码直接发生在此之前)

萨默拉德问

沃尔特,这是否意味着我们现在实际上可以实施概念精简版?

沃尔特对此做出了回应

哦耶!我已经做到了......它没有完全相同的语法。

我理解这次交流是关于 Concepts Lite 的。这种模式真的那么通用吗?无论出于何种原因,我都没有看到它。有人可以解释(或草图)这样的东西看起来如何吗?这只是关于enable_if和定义特征,还是提问者指的是什么?

void_t模板定义如下:

template<class ...> using void_t = void;

然后他使用它来检测类型语句是否格式正确,并使用它来实现is_copy_assignable类型特征:

//helper type
template<class T>
using copy_assignment_t
= decltype(declval<T&>() = declval<T const&>());

//base case template
template<class T, class=void>
struct is_copy_assignable : std::false_type {};

//SFINAE version only for types where copy_assignment_t<T> is well-formed.
template<class T>
struct is_copy_assignable<T, void_t<copy_assignment_t<T>>> 
: std::is_same<copy_assignment_t<T>,T&> {};

因为谈话,我理解了这个例子是如何工作的,但我不明白我们是如何从这里得到像 Concepts Lite 这样的东西的。

4

1 回答 1

120

是的,概念精简版基本上装扮了 SFINAE。此外,它允许更深入的内省以实现更好的超载。但是,这仅在概念谓词定义为时才有效concept bool。改进的重载不适用于当前的概念谓词,但可以使用条件重载。让我们看看如何在 C++14 中定义谓词、约束模板和重载函数。这有点长,但它介绍了如何在 C++14 中创建完成此任务所需的所有工具。

定义谓词

首先,用 all thestd::declvaldecltype到处来阅读谓词有点难看。相反,我们可以利用这样一个事实,即我们可以使用尾随 decltype(来自 Eric Niebler 的博客文章here)来约束函数,如下所示:

struct Incrementable
{
    template<class T>
    auto requires_(T&& x) -> decltype(++x);
};

因此,如果++x无效,则该requires_成员函数不可调用。因此,我们可以创建一个models仅检查是否requires_可调用的特征void_t

template<class Concept, class Enable=void>
struct models
: std::false_type
{};

template<class Concept, class... Ts>
struct models<Concept(Ts...), void_t< 
    decltype(std::declval<Concept>().requires_(std::declval<Ts>()...))
>>
: std::true_type
{};

约束模板

所以当我们想要基于概念约束模板时,我们仍然需要使用enable_if,但是我们可以使用这个宏来帮助它变得更简洁:

#define REQUIRES(...) typename std::enable_if<(__VA_ARGS__), int>::type = 0

所以我们可以定义一个increment基于Incrementable概念约束的函数:

template<class T, REQUIRES(models<Incrementable(T)>())>
void increment(T& x)
{
    ++x;
}

因此,如果我们调用increment不是 的东西Incrementable,我们将收到如下错误:

test.cpp:23:5: error: no matching function for call to 'incrementable'
    incrementable(f);
    ^~~~~~~~~~~~~
test.cpp:11:19: note: candidate template ignored: disabled by 'enable_if' [with T = foo]
template<class T, REQUIRES(models<Incrementable(T)>())>
                  ^

重载函数

现在如果我们想做重载,我们想使用条件重载。假设我们想创建一个std::advance使用概念谓词,我们可以这样定义它(现在我们将忽略可递减的情况):

struct Incrementable
{
    template<class T>
    auto requires_(T&& x) -> decltype(++x);
};

struct Advanceable
{
    template<class T, class I>
    auto requires_(T&& x, I&& i) -> decltype(x += i);
};

template<class Iterator, REQUIRES(models<Advanceable(Iterator, int)>())>
void advance(Iterator& it, int n)
{
    it += n;
}

template<class Iterator, REQUIRES(models<Incrementable(Iterator)>())>
void advance(Iterator& it, int n)
{
    while (n--) ++it;
}

concept bool然而,当它与std::vector迭代器一起使用时,这会导致模棱两可的重载(在精简概念中,这仍然是模棱两可的重载,除非我们将谓词更改为引用 a 中的其他谓词)。我们要做的是对调用进行排序,我们可以使用条件重载来做到这一点。可以考虑写这样的东西(这不是有效的C++):

template<class Iterator>
void advance(Iterator& it, int n) if (models<Advanceable(Iterator, int)>())
{
    it += n;
} 
else if (models<Incrementable(Iterator)>())
{
    while (n--) ++it;
}

所以如果第一个函数没有被调用,它将调用下一个函数。因此,让我们首先为两个功能实现它。我们将创建一个名为的类basic_conditional,它接受两个函数对象作为模板参数:

struct Callable
{
    template<class F, class... Ts>
    auto requires_(F&& f, Ts&&... xs) -> decltype(
        f(std::forward<Ts>(xs)...)
    );
};

template<class F1, class F2>
struct basic_conditional
{
    // We don't need to use a requires clause here because the trailing
    // `decltype` will constrain the template for us.
    template<class... Ts>
    auto operator()(Ts&&... xs) -> decltype(F1()(std::forward<Ts>(xs)...))
    {
        return F1()(std::forward<Ts>(xs)...);
    }
    // Here we add a requires clause to make this function callable only if
    // `F1` is not callable.
    template<class... Ts, REQUIRES(!models<Callable(F1, Ts&&...)>())>
    auto operator()(Ts&&... xs) -> decltype(F2()(std::forward<Ts>(xs)...))
    {
        return F2()(std::forward<Ts>(xs)...);
    }
};

所以现在这意味着我们需要将函数定义为函数对象:

struct advance_advanceable
{
    template<class Iterator, REQUIRES(models<Advanceable(Iterator, int)>())>
    void operator()(Iterator& it, int n) const
    {
        it += n;
    }
};

struct advance_incrementable
{
    template<class Iterator, REQUIRES(models<Incrementable(Iterator)>())>
    void operator()(Iterator& it, int n) const
    {
        while (n--) ++it;
    }
};

static conditional<advance_advanceable, advance_incrementable> advance = {};

所以现在如果我们尝试使用它std::vector

std::vector<int> v = { 1, 2, 3, 4, 5, 6 };
auto iterator = v.begin();
advance(iterator, 4);
std::cout << *iterator << std::endl;

它将编译并打印出来5

然而,std::advance实际上有三个重载,所以我们可以使用basic_conditional来实现conditional,它适用于使用递归的任意数量的函数:

template<class F, class... Fs>
struct conditional : basic_conditional<F, conditional<Fs...>>
{};

template<class F>
struct conditional<F> : F
{};

所以,现在我们可以这样写std::advance

struct Incrementable
{
    template<class T>
    auto requires_(T&& x) -> decltype(++x);
};

struct Decrementable
{
    template<class T>
    auto requires_(T&& x) -> decltype(--x);
};

struct Advanceable
{
    template<class T, class I>
    auto requires_(T&& x, I&& i) -> decltype(x += i);
};

struct advance_advanceable
{
    template<class Iterator, REQUIRES(models<Advanceable(Iterator, int)>())>
    void operator()(Iterator& it, int n) const
    {
        it += n;
    }
};

struct advance_decrementable
{
    template<class Iterator, REQUIRES(models<Decrementable(Iterator)>())>
    void operator()(Iterator& it, int n) const
    {
        if (n > 0) while (n--) ++it;
        else 
        {
            n *= -1;
            while (n--) --it;
        }
    }
};

struct advance_incrementable
{
    template<class Iterator, REQUIRES(models<Incrementable(Iterator)>())>
    void operator()(Iterator& it, int n) const
    {
        while (n--) ++it;
    }
};

static conditional<advance_advanceable, advance_decrementable, advance_incrementable> advance = {};

使用 Lambda 重载

但是,此外,我们可以使用 lambdas 来代替函数对象来编写它,这有助于使其编写起来更简洁。所以我们STATIC_LAMBDA在编译时使用这个宏来构造 lambda:

struct wrapper_factor
{
    template<class F>
    constexpr wrapper<F> operator += (F*)
    {
        return {};
    }
};

struct addr_add
{
    template<class T>
    friend typename std::remove_reference<T>::type *operator+(addr_add, T &&t) 
    {
        return &t;
    }
};

#define STATIC_LAMBDA wrapper_factor() += true ? nullptr : addr_add() + []

并添加一个make_conditional函数constexpr

template<class... Fs>
constexpr conditional<Fs...> make_conditional(Fs...)
{
    return {};
}

然后我们现在可以advance像这样编写函数:

constexpr const advance = make_conditional(
    STATIC_LAMBDA(auto& it, int n, REQUIRES(models<Advanceable(decltype(it), int)>()))
    {
        it += n;
    },
    STATIC_LAMBDA(auto& it, int n, REQUIRES(models<Decrementable(decltype(it))>()))
    {
        if (n > 0) while (n--) ++it;
        else 
        {
            n *= -1;
            while (n--) --it;
        }
    },
    STATIC_LAMBDA(auto& it, int n, REQUIRES(models<Incrementable(decltype(it))>()))
    {
        while (n--) ++it;
    }
);

这比使用函数对象版本更紧凑和可读。

此外,我们可以定义一个modeled函数来减少decltype丑陋:

template<class Concept, class... Ts>
constexpr auto modeled(Ts&&...)
{
    return models<Concept(Ts...)>();
}

constexpr const advance = make_conditional(
    STATIC_LAMBDA(auto& it, int n, REQUIRES(modeled<Advanceable>(it, n)))
    {
        it += n;
    },
    STATIC_LAMBDA(auto& it, int n, REQUIRES(modeled<Decrementable>(it)))
    {
        if (n > 0) while (n--) ++it;
        else 
        {
            n *= -1;
            while (n--) --it;
        }
    },
    STATIC_LAMBDA(auto& it, int n, REQUIRES(modeled<Incrementable>(it)))
    {
        while (n--) ++it;
    }
);

最后,如果您有兴趣使用现有的库解决方案(而不是像我展示的那样滚动您自己的解决方案)。Tick库提供了定义概念和约束模板的框架。Fit库可以处理函数和重载。

于 2014-10-23T16:56:29.730 回答