【发布时间】:2013-04-21 04:47:15
【问题描述】:
这是对my previous post 的复述,因为我更改了问题(因此它可能没有被标记为新问题而被遗漏)。我也希望能把它剪掉。
我有这样的功能:
#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 元函数给定次数一样。
当我尝试运行该程序时,我遇到了一堆错误,例如:“来自Whatever(*)[Y] 类型的表达式的Whatever(&)[X][Y] 类型的引用无效初始化”(或Whatever(&)[Z] 来自Whatever*)。我的解决方法是将条件表达式转换为 if-else 对(并删除 constexpr)。
我正在尝试找出问题所在,因此我正在浏览有关 C++ (2011) 标准中条件运算符的部分。这是第 5.16 节。当两个可能的操作之一是 throw 命令(或者是 void 表达式)时,条件具有另一个表达式的类型,但标准转换,包括数组到指针,适用于另一个表达。 (这在第 2 段中。)我认为这让我感到困惑。有什么办法吗?我认为返回数组引用会抑制 a-p 转换。为什么做成if/else会起作用?
【问题讨论】:
标签: c++ arrays c++11 conditional-operator throw