Constexpr使用C ++ 17查找数组 [英] Constexpr find for array using c++17

查看:119
本文介绍了Constexpr使用C ++ 17查找数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写constexpr查找函数,该函数将返回包含特定值的std :: array的索引。下面的函数似乎可以正常工作,除非包含的类型为 const char *

I am trying to write a constexpr find function that will return the index of a std::array containing a certain value. The function below seems to work OK except when the contained type is const char*:

#include <array>

constexpr auto name1() {
    return "name1";
}

constexpr auto name2() {
    return "name2";
}

template <class X, class V>
constexpr auto find(X& x, V key) {
    std::size_t i = 0;
    while(i < x.size()) {
        if(x[i] == key) return i;
        ++i;
    }

    return i;
}


int main() {

    constexpr std::array<const char*, 2> x{{name1(), name2()}};
    constexpr auto f1 = find(x, name1()); // this compiles
    constexpr auto f2 = find(x, name2()); // this doesn't...
}

奇怪的是, find(x,name1())可以干净地编译,但 find(x,name2())失败并显示错误:

The weird thing is that find(x, name1()) compiles cleanly but find(x, name2()) fails with the error:

subexpression not valid in a constant expression
        if(x[i] == key) return i; `

name1(),但与 name2()一起使用会失败?

How can this expression work when used with name1() but fail when used with name2()?

我也发现了这个答案,但是用户从头开始构建数组类,我不想这样做。

I have also found this answer, but the user builds the array class from scratch and I do not want to do that.

推荐答案

似乎是编译器错误。 f1 f2 应该都无法以相同的方式编译。

Seems like a compiler bug. Both f1 and f2 should fail to compile in the same way.

主要问题是它是一个假设,其中 name1 == name1 name1 != name2 。该标准实际上不提供此类保证,请参见 [lex.string] / 16

The main issue is that it's an assumption that "name1" == "name1" and "name1" != "name2". The standard in fact provides no such guarantees, see [lex.string]/16:


是否所有字符串文字都是不同的(即存储在非重叠对象中)以及是否连续对 string-literal 产生的对象相同或不同

Whether all string literals are distinct (that is, are stored in nonoverlapping objects) and whether successive evaluations of a string-literal yield the same or a different object is unspecified.

即使假设最有可能成立,也要比较 constexpr 明确不允许,请参见 [expr.const] /2.23

Even though the assumption most likely holds, comparing unspecified values inside constexpr is expressly not allowed, see [expr.const]/2.23:


—关系( [expr.rel] )或相等( [expr.eq] )运算符,未指定结果;

— a relational ([expr.rel]) or equality ([expr.eq]) operator where the result is unspecified;

一种解决方法(正确的做法)是不依赖字符串文字的地址,而是比较实际的字符串。例如:

A workaround (and the right thing to do) would be to not rely on addresses of string literals and instead compare the actual strings. For example:

constexpr bool equals(const char* a, const char* b) {
    for (std::size_t i = 0; ; ++i) {
        if (a[i] != b[i]) return false;
        if (a[i] == 0) break;
    }
    return true;
}

template <class X, class V>
constexpr auto find(X& x, V key) {
    std::size_t i = 0;
    while(i < x.size()) {
        if(equals(x[i], key)) return i;
        ++i;
    }

    return i;
}

这篇关于Constexpr使用C ++ 17查找数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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