数组如何使用条件运算符? [英] How can an array work with the conditional operator?

查看:175
本文介绍了数组如何使用条件运算符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个重述的我的上一篇文章,因为我改变了问题(所以它可能没有被标记为一个新的问题,并错过了)。

This is a retelling of my previous post, since I changed the question (so it probably didn't get flagged as a new question and was missed). I'll hopefully trim it down too.

我有以下功能:

#include <cstddef>
#include <type_traits>

template < typename E, typename T >
inline constexpr
auto  checked_slice( E &&, T &&t ) noexcept -> T &&
{ return static_cast<T &&>(t); }

template < typename E, typename T, std::size_t N, typename U, typename ...V >
inline constexpr
auto  checked_slice( E &&e, T (&t)[N], U &&u, V &&...v )
 -> typename remove_some_extents<T, sizeof...(V)>::type &
{
    typedef typename std::remove_reference<U>::type                 u_type;
    typedef typename std::common_type<u_type, std::size_t>::type  cmp_type;

    return ( u < u_type{} ) || ( static_cast<cmp_type>(u) >=
     static_cast<cmp_type>(N) ) ? throw e : checked_slice( static_cast<E &&>(e),
     t[static_cast<U &&>( u )], static_cast<V &&>(v)... );
}

其中 remove_some_extents 一个自定义类模板,就像调用 std :: remove_extent 元函数给定的次数。

where remove_some_extents is a custom class template that's like calling the std::remove_extent meta-function a given number of times.

我试过运行程序,我得到一堆错误像:无效的引用的初始化 Whatever(&)[X] [Y] 从类型 Whatever(*)[Y] (或 Whatever(&)[Z] )。我的解决方法是将条件表达式转换为如果 - else 对(并删除 constexpr )。

When I tried running the program, I got a bunch of errors like: "invalid initialization of reference of type Whatever(&)[X][Y] from expression of type Whatever(*)[Y]" (or Whatever(&)[Z] from Whatever*). My workaround was to convert the conditional expression to an if-else pair (and removing the constexpr).

我想弄清楚出了什么问题,所以我在关于C ++中的条件运算符的部分(2011)标准。这是第5.16节。当两个可能的动作之一是throw命令(或否则是 void 表达式)时,条件具有另一个表达式的类型,但标准转换,包括数组到指针,应用于该其他表达式。 (这是在第2段)我认为这是什么东西搞砸了我。有什么办法吗?我认为返回一个数组引用抑制了a到p的转换。为什么在 if / else

I'm trying to figure out what's wrong, so I'm poking around the section about the conditional operator in the C++ (2011) standard. That's section 5.16. When one of the two possible actions is a throw command (or is otherwise a void expression), then the conditional has the type of the other expression, but the standard conversions, including array-to-pointer, is applied to that other expression. (This is in paragraph 2.) I think that's what's messing me up. Is there any way around it? I thought returning an array reference suppresses the a-to-p conversion. Why does it work when made into an if/else?

推荐答案

您的分析是正确的。我怀疑在这种情况下非 - void 操作数是衰变的(即执行通常的转换),以模仿当两个操作数不同时发生的情况

Your analysis is correct. I suspect that the non-void operand is 'decayed' (that is, the usual conversions are performed) in such a situation so as to mimic what happens when the two operands differ in types -- in the latter case more often than not the whole conditional expression is a prvalue.

在一种情况下,我们知道的值类型和类型的一个条件表达式是两个操作数完全匹配的时候,所以我们可以使用它来获得我们的优势:

One situation in which we know for sure both value category and type of a conditional expression is when the two operands are exact matches, so we can use that to our advantage:

cond ? (throw e, t) : t

将是数组引用类型的左值。 (当然,最后一个操作数不必是字面上 t - 你可以在这里插入你的递归调用。)

will be an lvalue of array reference type. (Of course the last operand doesn't have to be literally t -- you can plug your recursive call here just fine.)

如果如果 / else ,则没有遇到任何这样的障碍,因为语言不必为其指定公共类型和值类别。

You did not encounter any such hurdle when using if/else because as a statement the language does not have to specify a common type and value category for it.

这篇关于数组如何使用条件运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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