为什么不标准::算法constexpr,哪些可能呢? [英] Why aren't std::algorithms constexpr and which could be?

查看:120
本文介绍了为什么不标准::算法constexpr,哪些可能呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么没有任何的std ::算法方法 constexpr ?如果我没有理解新的C ++ 14的规则,很多这些方法可能是 constexpr 。例如,为什么不能的std ::发现 constexpr

Why aren't any std::algorithm methods constexpr? If I understand the new C++14 rules correctly, many of these methods could be constexpr. For example, why can't std::find be constexpr?

static constexpr std::array<char, 4> DnaBases {'A', 'C', 'G', 'T'};

constexpr bool is_dna(char b)
{
    return std::find(std::cbegin(DnaBases), std::cend(DnaBases), b) != std::cend(DnaBases); // why not?
}

还有哪些的std ::算法取值可以是 constexpr

推荐答案

这可能是 constexpr ,但不能被评价为一个常数前pression,因为在这种情况下,例如用于编译时发现,要求: 开始/结束应该是constexpr,在 *运算符的迭代器应该是constexpr,运营商== 应该是constexpr,操作符!= 的迭代器应该是constexpr,符++ 的迭代器应该是constexpr。但是,如果所有的功能 constexpr ,那么很多算法可以用 constexpr 实施。

It could be constexpr, but cannot be evaluated as a constant expression, since in this case, for example for compile-time find it is required that: begin/end should be constexpr, the * operator of the iterator should be constexpr, operator == should be constexpr, operator != for the iterator should be constexpr, operator ++ for the iterator should be constexpr. But, if all functions are constexpr, then many algorithms can be implemented with constexpr.

您可以看看 SPROUT库为constexpr实施容器/算法。

You can look at the SPROUT library for the implementation of constexpr containers/algorithms.

而在 isocpp.org论坛

这篇关于为什么不标准::算法constexpr,哪些可能呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆