如何一个阵列,条件运算符的工作? [英] How can an array work with the conditional operator?
问题描述
这是的我的previous帖子,因为我改变了这个问题(所以它可能没有得到标记为一个新的问题和被错过)。我会希望修剪下来了。
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.
当我试图运行程序,我有一帮喜欢的错误:类型的引用无效初始化从型的前pression
(或
从
)。我的解决方法是有条件的前pression转换为
如果
- 其他
对(并删除 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。当两个可能的动作之一是掷命令(或以其他方式一空隙
前pression),那么条件的另一前$ P $的类型pssion,但标准的转换,包括阵列到指针,将应用于其他前pression。 (这是第2款),我认为这就是搞乱了我。有没有办法解决它的任何方式吗?我以为返回数组引用燮presses的一对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
?
推荐答案
您的分析是正确的。我猜想非 - 无效在这种情况下,以模仿当两个操作数的不同,会发生什么(也就是通常的转换执行)
操作数'烂'在类型 - 在后一种情况下,往往不是整个有条件前pression是一个prvalue
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.
中,我们知道一个有条件的前pression的肯定价值类别和类型都一种情况是,当两个操作数完全匹配,所以我们可以用它来我们的优势:
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.)
使用时,您没有遇到任何这样的障碍如果
/ 其他
因为作为一个语句语言没有指定一个常见的类型和价值类型吧。
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屋!