你能在条件表达式中添加条件表达式吗? (是:边界检查如何扩展到多个维度?)

2024-04-20

Note:我通过实现一个完全不同的问题来解决最初的问题。有关新的实际问题,请参阅附录,但您可以阅读前一部分以了解上下文。

这是我的一个的扩展以前的帖子 https://stackoverflow.com/q/10171525/1010226。我根据这个答案创建了一个容器类:

template < typename T, unsigned N0, unsigned ...N >
struct array_md
{
    // There's a class template specialization with no extents.

    // Imagine the various N... components are bracket-enclosed instead
    // of comma-separated.  And if "N..." is empty, then we just have "T"
    // as the "direct_element_type".  (I actually use recursive class
    // definitions.)
    using direct_element_type = T[N...];
    using data_type = direct_element_type[ N0 ];

    template < typename ...Indices >
    auto  operator ()( Indices &&...i )
    noexcept( !indexing_result<data_type &, Indices...>::can_throw )
    -> typename indexing_result<data_type &, Indices...>::type
    { return slice(data, static_cast<Indices &&>( i )...); }

    template < typename ...Indices >
    constexpr
    auto  operator ()( Indices &&...i ) const
    noexcept( !indexing_result<data_type &, Indices...>::can_throw )
    -> typename indexing_result<data_type &, Indices...>::type
    { return slice(data, static_cast<Indices &&>( i )...); }

    data_type  data;
};

我正在尝试制作一个版本at对于这个容器。我想我只会制作一个版本slice需要一个异常对象。与一般的不同slice, my checked_slice必须接受内置数组对象,因为指针和类类型(带有operator [])没有(标准)方法给我界限。

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 ...V >
inline constexpr
auto  checked_slice( E &&e, T (&t)[N], std::size_t u, V &&...v )
-> typename remove_some_extents<T[N], 1u + sizeof...(V)>::type &
{
    return u < N ? checked_slice( static_cast<E &&>(e), static_cast<T &>(t[ u ]),
     static_cast<V &&>(v)... ) : throw static_cast<E &&>( e );
}

template < typename E, typename T, std::size_t N, typename ...V >
inline constexpr
auto  checked_slice( E &&e, T (&&t)[N], std::size_t u, V &&...v )
-> typename remove_some_extents<T[N], 1u + sizeof...(V)>::type &&
{
    return u < N ? checked_slice( static_cast<E &&>(e),static_cast<T &&>(t[ u ]),
     static_cast<V &&>(v)... ) : throw static_cast<E &&>( e );
}

(The remove_some_extents按照它所说的去做,而不是仅仅 C++11 标准库为您提供的一个或全部。)当我将其放入我的at:

template < typename T, unsigned N0, unsigned ...N >
struct array_md
{
    //...
    template < typename ...Indices >
    auto  at( Indices &&...i )
    -> typename remove_some_extents<data_type, sizeof...( Indices )>::type &
    {
        return checked_slice(std::out_of_range{ "Index out of bounds" }, data,
         static_cast<Indices &&>( i )...);
    }

    template < typename ...Indices >
    constexpr
    auto  at( Indices &&...i ) const
    -> typename remove_some_extents<data_type,sizeof...( Indices )>::type const &
    {
        return checked_slice(std::out_of_range{ "Index out of bounds" }, data,
         static_cast<Indices &&>( i )...);
    }
    //...
};

我收到与数组到指针衰减相关的错误! (我使用的是 TDC-GCC 4.7.1,它与适用于 Windows-8 Pro 32 位的 CodeBlocks 12.11 捆绑在一起。)

In file included from container/array_md.hpp:36:0,
                 from test\arraymd_test.cpp:15:
utility/slice.hpp: In instantiation of 'constexpr typename remove_some_extents<T [N], (1u + sizeof (V ...))>::type& checked_slice(E&&, T (&)[N], std::size_t, V&& ...) [with E = std::out_of_range; T = const int; unsigned int N = 2u; V = {}; typename remove_some_extents<T [N], (1u + sizeof (V ...))>::type = const int; std::size_t = unsigned int]':
container/array_md.hpp:284:112:   required from 'constexpr const typename remove_some_extents<typename container::array_md<T, N ...>::data_type [M], sizeof (Indices ...)>::type& container::array_md<T, M, N ...>::at(Indices&& ...) const [with Indices = {int}; T = int; unsigned int M = 2u; unsigned int ...N = {}; typename remove_some_extents<typename container::array_md<T, N ...>::data_type [M], sizeof (Indices ...)>::type = int]'
test\arraymd_test.cpp:224:1:   required from here
utility/slice.hpp:141:10: warning: returning reference to temporary [enabled by default]
utility/slice.hpp: In instantiation of 'constexpr typename remove_some_extents<T [N], (1u + sizeof (V ...))>::type& checked_slice(E&&, T (&)[N], std::size_t, V&& ...) [with E = std::out_of_range; T = const char [2][6]; unsigned int N = 2u; V = {}; typename remove_some_extents<T [N], (1u + sizeof (V ...))>::type = const char [2][6]; std::size_t = unsigned int]':
container/array_md.hpp:284:112:   required from 'constexpr const typename remove_some_extents<typename container::array_md<T, N ...>::data_type [M], sizeof (Indices ...)>::type& container::array_md<T, M, N ...>::at(Indices&& ...) const [with Indices = {int}; T = char [6]; unsigned int M = 2u; unsigned int ...N = {2u}; typename remove_some_extents<typename container::array_md<T, N ...>::data_type [M], sizeof (Indices ...)>::type = char [2][6]]'
test\arraymd_test.cpp:238:1:   required from here
utility/slice.hpp:141:10: error: invalid initialization of reference of type 'const char (&)[2][6]' from expression of type 'const char (*)[6]'
utility/slice.hpp:142:1: error: body of constexpr function 'constexpr typename remove_some_extents<T [N], (1u + sizeof (V ...))>::type& checked_slice(E&&, T (&)[N], std::size_t, V&& ...) [with E = std::out_of_range; T = const char [2][6]; unsigned int N = 2u; V = {}; typename remove_some_extents<T [N], (1u + sizeof (V ...))>::type = const char [2][6]; std::size_t = unsigned int]' not a return-statement
utility/slice.hpp: In instantiation of 'constexpr typename remove_some_extents<T [N], (1u + sizeof (V ...))>::type& checked_slice(E&&, T (&)[N], std::size_t, V&& ...) [with E = std::out_of_range; T = const char [6]; unsigned int N = 2u; V = {}; typename remove_some_extents<T [N], (1u + sizeof (V ...))>::type = const char [6]; std::size_t = unsigned int]':
utility/slice.hpp:141:10:   required from 'constexpr typename remove_some_extents<T [N], (1u + sizeof (V ...))>::type& checked_slice(E&&, T (&)[N], std::size_t, V&& ...) [with E = std::out_of_range; T = const char [2][6]; unsigned int N = 2u; V = {int}; typename remove_some_extents<T [N], (1u + sizeof (V ...))>::type = const char [6]; std::size_t = unsigned int]'
container/array_md.hpp:284:112:   required from 'constexpr const typename remove_some_extents<typename container::array_md<T, N ...>::data_type [M], sizeof (Indices ...)>::type& container::array_md<T, M, N ...>::at(Indices&& ...) const [with Indices = {int, int}; T = char [6]; unsigned int M = 2u; unsigned int ...N = {2u}; typename remove_some_extents<typename container::array_md<T, N ...>::data_type [M], sizeof (Indices ...)>::type = char [6]]'
test\arraymd_test.cpp:239:1:   required from here
utility/slice.hpp:141:10: error: invalid initialization of reference of type 'const char (&)[6]' from expression of type 'const char*'
utility/slice.hpp:142:1: error: body of constexpr function 'constexpr typename remove_some_extents<T [N], (1u + sizeof (V ...))>::type& checked_slice(E&&, T (&)[N], std::size_t, V&& ...) [with E = std::out_of_range; T = const char [6]; unsigned int N = 2u; V = {}; typename remove_some_extents<T [N], (1u + sizeof (V ...))>::type = const char [6]; std::size_t = unsigned int]' not a return-statement
utility/slice.hpp: In instantiation of 'constexpr typename remove_some_extents<T [N], (1u + sizeof (V ...))>::type& checked_slice(E&&, T (&)[N], std::size_t, V&& ...) [with E = std::out_of_range; T = const char [2][6]; unsigned int N = 2u; V = {int}; typename remove_some_extents<T [N], (1u + sizeof (V ...))>::type = const char [6]; std::size_t = unsigned int]':
container/array_md.hpp:284:112:   required from 'constexpr const typename remove_some_extents<typename container::array_md<T, N ...>::data_type [M], sizeof (Indices ...)>::type& container::array_md<T, M, N ...>::at(Indices&& ...) const [with Indices = {int, int}; T = char [6]; unsigned int M = 2u; unsigned int ...N = {2u}; typename remove_some_extents<typename container::array_md<T, N ...>::data_type [M], sizeof (Indices ...)>::type = char [6]]'
test\arraymd_test.cpp:239:1:   required from here
utility/slice.hpp:141:10: error: invalid initialization of reference of type 'const char (&)[6]' from expression of type 'const char*'
utility/slice.hpp:142:1: error: body of constexpr function 'constexpr typename remove_some_extents<T [N], (1u + sizeof (V ...))>::type& checked_slice(E&&, T (&)[N], std::size_t, V&& ...) [with E = std::out_of_range; T = const char [2][6]; unsigned int N = 2u; V = {int}; typename remove_some_extents<T [N], (1u + sizeof (V ...))>::type = const char [6]; std::size_t = unsigned int]' not a return-statement
utility/slice.hpp: In instantiation of 'constexpr typename remove_some_extents<T [N], (1u + sizeof (V ...))>::type& checked_slice(E&&, T (&)[N], std::size_t, V&& ...) [with E = std::out_of_range; T = const char [2][6]; unsigned int N = 2u; V = {unsigned int}; typename remove_some_extents<T [N], (1u + sizeof (V ...))>::type = const char [6]; std::size_t = unsigned int]':
container/array_md.hpp:284:112:   required from 'constexpr const typename remove_some_extents<typename container::array_md<T, N ...>::data_type [M], sizeof (Indices ...)>::type& container::array_md<T, M, N ...>::at(Indices&& ...) const [with Indices = {int, unsigned int}; T = char [6]; unsigned int M = 2u; unsigned int ...N = {2u}; typename remove_some_extents<typename container::array_md<T, N ...>::data_type [M], sizeof (Indices ...)>::type = char [6]]'
test\arraymd_test.cpp:261:5:   required from here
utility/slice.hpp:141:10: error: invalid initialization of reference of type 'const char (&)[6]' from expression of type 'const char*'
utility/slice.hpp:142:1: error: body of constexpr function 'constexpr typename remove_some_extents<T [N], (1u + sizeof (V ...))>::type& checked_slice(E&&, T (&)[N], std::size_t, V&& ...) [with E = std::out_of_range; T = const char [2][6]; unsigned int N = 2u; V = {unsigned int}; typename remove_some_extents<T [N], (1u + sizeof (V ...))>::type = const char [6]; std::size_t = unsigned int]' not a return-statement
utility/slice.hpp: In instantiation of 'constexpr typename remove_some_extents<T [N], (1u + sizeof (V ...))>::type& checked_slice(E&&, T (&)[N], std::size_t, V&& ...) [with E = std::out_of_range; T = int; unsigned int N = 2u; V = {}; typename remove_some_extents<T [N], (1u + sizeof (V ...))>::type = int; std::size_t = unsigned int]':
container/array_md.hpp:274:112:   required from 'typename remove_some_extents<typename container::array_md<T, N ...>::data_type [M], sizeof (Indices ...)>::type& container::array_md<T, M, N ...>::at(Indices&& ...) [with Indices = {int}; T = int; unsigned int M = 2u; unsigned int ...N = {}; typename remove_some_extents<typename container::array_md<T, N ...>::data_type [M], sizeof (Indices ...)>::type = int]'
test\arraymd_test.cpp:220:1:   required from here
utility/slice.hpp:141:10: error: invalid initialization of non-const reference of type 'remove_some_extents<int [2], 1u>::type& {aka int&}' from an rvalue of type 'int'
utility/slice.hpp:142:1: error: body of constexpr function 'constexpr typename remove_some_extents<T [N], (1u + sizeof (V ...))>::type& checked_slice(E&&, T (&)[N], std::size_t, V&& ...) [with E = std::out_of_range; T = int; unsigned int N = 2u; V = {}; typename remove_some_extents<T [N], (1u + sizeof (V ...))>::type = int; std::size_t = unsigned int]' not a return-statement
utility/slice.hpp: In instantiation of 'constexpr typename remove_some_extents<T [N], (1u + sizeof (V ...))>::type& checked_slice(E&&, T (&)[N], std::size_t, V&& ...) [with E = std::out_of_range; T = char [2][6]; unsigned int N = 2u; V = {}; typename remove_some_extents<T [N], (1u + sizeof (V ...))>::type = char [2][6]; std::size_t = unsigned int]':
container/array_md.hpp:274:112:   required from 'typename remove_some_extents<typename container::array_md<T, N ...>::data_type [M], sizeof (Indices ...)>::type& container::array_md<T, M, N ...>::at(Indices&& ...) [with Indices = {int}; T = char [6]; unsigned int M = 2u; unsigned int ...N = {2u}; typename remove_some_extents<typename container::array_md<T, N ...>::data_type [M], sizeof (Indices ...)>::type = char [2][6]]'
test\arraymd_test.cpp:234:1:   required from here
utility/slice.hpp:141:10: error: invalid initialization of non-const reference of type 'char (&)[2][6]' from an rvalue of type 'char (*)[6]'
utility/slice.hpp:142:1: error: body of constexpr function 'constexpr typename remove_some_extents<T [N], (1u + sizeof (V ...))>::type& checked_slice(E&&, T (&)[N], std::size_t, V&& ...) [with E = std::out_of_range; T = char [2][6]; unsigned int N = 2u; V = {}; typename remove_some_extents<T [N], (1u + sizeof (V ...))>::type = char [2][6]; std::size_t = unsigned int]' not a return-statement
utility/slice.hpp: In instantiation of 'constexpr typename remove_some_extents<T [N], (1u + sizeof (V ...))>::type& checked_slice(E&&, T (&)[N], std::size_t, V&& ...) [with E = std::out_of_range; T = char [6]; unsigned int N = 2u; V = {}; typename remove_some_extents<T [N], (1u + sizeof (V ...))>::type = char [6]; std::size_t = unsigned int]':
utility/slice.hpp:141:10:   required from 'constexpr typename remove_some_extents<T [N], (1u + sizeof (V ...))>::type& checked_slice(E&&, T (&)[N], std::size_t, V&& ...) [with E = std::out_of_range; T = char [2][6]; unsigned int N = 2u; V = {int}; typename remove_some_extents<T [N], (1u + sizeof (V ...))>::type = char [6]; std::size_t = unsigned int]'
container/array_md.hpp:274:112:   required from 'typename remove_some_extents<typename container::array_md<T, N ...>::data_type [M], sizeof (Indices ...)>::type& container::array_md<T, M, N ...>::at(Indices&& ...) [with Indices = {int, int}; T = char [6]; unsigned int M = 2u; unsigned int ...N = {2u}; typename remove_some_extents<typename container::array_md<T, N ...>::data_type [M], sizeof (Indices ...)>::type = char [6]]'
test\arraymd_test.cpp:235:1:   required from here
utility/slice.hpp:141:10: error: invalid initialization of non-const reference of type 'char (&)[6]' from an rvalue of type 'char*'
utility/slice.hpp:142:1: error: body of constexpr function 'constexpr typename remove_some_extents<T [N], (1u + sizeof (V ...))>::type& checked_slice(E&&, T (&)[N], std::size_t, V&& ...) [with E = std::out_of_range; T = char [6]; unsigned int N = 2u; V = {}; typename remove_some_extents<T [N], (1u + sizeof (V ...))>::type = char [6]; std::size_t = unsigned int]' not a return-statement
utility/slice.hpp: In instantiation of 'constexpr typename remove_some_extents<T [N], (1u + sizeof (V ...))>::type& checked_slice(E&&, T (&)[N], std::size_t, V&& ...) [with E = std::out_of_range; T = char [2][6]; unsigned int N = 2u; V = {int}; typename remove_some_extents<T [N], (1u + sizeof (V ...))>::type = char [6]; std::size_t = unsigned int]':
container/array_md.hpp:274:112:   required from 'typename remove_some_extents<typename container::array_md<T, N ...>::data_type [M], sizeof (Indices ...)>::type& container::array_md<T, M, N ...>::at(Indices&& ...) [with Indices = {int, int}; T = char [6]; unsigned int M = 2u; unsigned int ...N = {2u}; typename remove_some_extents<typename container::array_md<T, N ...>::data_type [M], sizeof (Indices ...)>::type = char [6]]'
test\arraymd_test.cpp:235:1:   required from here
utility/slice.hpp:141:10: error: invalid initialization of non-const reference of type 'char (&)[6]' from an rvalue of type 'char*'
utility/slice.hpp:142:1: error: body of constexpr function 'constexpr typename remove_some_extents<T [N], (1u + sizeof (V ...))>::type& checked_slice(E&&, T (&)[N], std::size_t, V&& ...) [with E = std::out_of_range; T = char [2][6]; unsigned int N = 2u; V = {int}; typename remove_some_extents<T [N], (1u + sizeof (V ...))>::type = char [6]; std::size_t = unsigned int]' not a return-statement
utility/slice.hpp: In instantiation of 'constexpr typename remove_some_extents<T [N], (1u + sizeof (V ...))>::type& checked_slice(E&&, T (&)[N], std::size_t, V&& ...) [with E = std::out_of_range; T = char [2][6]; unsigned int N = 2u; V = {long double}; typename remove_some_extents<T [N], (1u + sizeof (V ...))>::type = char [6]; std::size_t = unsigned int]':
container/array_md.hpp:274:112:   required from 'typename remove_some_extents<typename container::array_md<T, N ...>::data_type [M], sizeof (Indices ...)>::type& container::array_md<T, M, N ...>::at(Indices&& ...) [with Indices = {int, long double}; T = char [6]; unsigned int M = 2u; unsigned int ...N = {2u}; typename remove_some_extents<typename container::array_md<T, N ...>::data_type [M], sizeof (Indices ...)>::type = char [6]]'
test\arraymd_test.cpp:260:5:   required from here
utility/slice.hpp:141:10: error: invalid initialization of non-const reference of type 'char (&)[6]' from an rvalue of type 'char*'
utility/slice.hpp:142:1: error: body of constexpr function 'constexpr typename remove_some_extents<T [N], (1u + sizeof (V ...))>::type& checked_slice(E&&, T (&)[N], std::size_t, V&& ...) [with E = std::out_of_range; T = char [2][6]; unsigned int N = 2u; V = {long double}; typename remove_some_extents<T [N], (1u + sizeof (V ...))>::type = char [6]; std::size_t = unsigned int]' not a return-statement
utility/slice.hpp: In function 'constexpr typename remove_some_extents<T [N], (1u + sizeof (V ...))>::type& checked_slice(E&&, T (&)[N], std::size_t, V&& ...) [with E = std::out_of_range; T = const char [2][6]; unsigned int N = 2u; V = {unsigned int}; typename remove_some_extents<T [N], (1u + sizeof (V ...))>::type = const char [6]; std::size_t = unsigned int]':
utility/slice.hpp:142:1: warning: control reaches end of non-void function [-Wreturn-type]
utility/slice.hpp: In function 'constexpr typename remove_some_extents<T [N], (1u + sizeof (V ...))>::type& checked_slice(E&&, T (&)[N], std::size_t, V&& ...) [with E = std::out_of_range; T = const char [2][6]; unsigned int N = 2u; V = {int}; typename remove_some_extents<T [N], (1u + sizeof (V ...))>::type = const char [6]; std::size_t = unsigned int]':
utility/slice.hpp:142:1: warning: control reaches end of non-void function [-Wreturn-type]
utility/slice.hpp: In function 'constexpr typename remove_some_extents<T [N], (1u + sizeof (V ...))>::type& checked_slice(E&&, T (&)[N], std::size_t, V&& ...) [with E = std::out_of_range; T = const char [6]; unsigned int N = 2u; V = {}; typename remove_some_extents<T [N], (1u + sizeof (V ...))>::type = const char [6]; std::size_t = unsigned int]':
utility/slice.hpp:142:1: warning: control reaches end of non-void function [-Wreturn-type]
utility/slice.hpp: In function 'constexpr typename remove_some_extents<T [N], (1u + sizeof (V ...))>::type& checked_slice(E&&, T (&)[N], std::size_t, V&& ...) [with E = std::out_of_range; T = const char [2][6]; unsigned int N = 2u; V = {}; typename remove_some_extents<T [N], (1u + sizeof (V ...))>::type = const char [2][6]; std::size_t = unsigned int]':
utility/slice.hpp:142:1: warning: control reaches end of non-void function [-Wreturn-type]
utility/slice.hpp: In function 'constexpr typename remove_some_extents<T [N], (1u + sizeof (V ...))>::type& checked_slice(E&&, T (&)[N], std::size_t, V&& ...) [with E = std::out_of_range; T = char [2][6]; unsigned int N = 2u; V = {long double}; typename remove_some_extents<T [N], (1u + sizeof (V ...))>::type = char [6]; std::size_t = unsigned int]':
utility/slice.hpp:142:1: warning: control reaches end of non-void function [-Wreturn-type]
utility/slice.hpp: In function 'constexpr typename remove_some_extents<T [N], (1u + sizeof (V ...))>::type& checked_slice(E&&, T (&)[N], std::size_t, V&& ...) [with E = std::out_of_range; T = char [2][6]; unsigned int N = 2u; V = {int}; typename remove_some_extents<T [N], (1u + sizeof (V ...))>::type = char [6]; std::size_t = unsigned int]':
utility/slice.hpp:142:1: warning: control reaches end of non-void function [-Wreturn-type]
utility/slice.hpp: In function 'constexpr typename remove_some_extents<T [N], (1u + sizeof (V ...))>::type& checked_slice(E&&, T (&)[N], std::size_t, V&& ...) [with E = std::out_of_range; T = char [6]; unsigned int N = 2u; V = {}; typename remove_some_extents<T [N], (1u + sizeof (V ...))>::type = char [6]; std::size_t = unsigned int]':
utility/slice.hpp:142:1: warning: control reaches end of non-void function [-Wreturn-type]
utility/slice.hpp: In function 'constexpr typename remove_some_extents<T [N], (1u + sizeof (V ...))>::type& checked_slice(E&&, T (&)[N], std::size_t, V&& ...) [with E = std::out_of_range; T = char [2][6]; unsigned int N = 2u; V = {}; typename remove_some_extents<T [N], (1u + sizeof (V ...))>::type = char [2][6]; std::size_t = unsigned int]':
utility/slice.hpp:142:1: warning: control reaches end of non-void function [-Wreturn-type]
utility/slice.hpp: In function 'constexpr typename remove_some_extents<T [N], (1u + sizeof (V ...))>::type& checked_slice(E&&, T (&)[N], std::size_t, V&& ...) [with E = std::out_of_range; T = int; unsigned int N = 2u; V = {}; typename remove_some_extents<T [N], (1u + sizeof (V ...))>::type = int; std::size_t = unsigned int]':
utility/slice.hpp:142:1: warning: control reaches end of non-void function [-Wreturn-type]

我认为使用数组引用可以取消数组到指针的衰减。这是 GCC 错误,还是我在某个地方搞砸了?

对于“slice.hpp”,第 141 行第 10 列是(左值版本)中语句的结尾checked_slice函数,而 L142C1 是函数的结束括号。在“array_md.hpp”中,L284C112 和 L274C112 是返回(也是唯一)语句at功能,const和非常量分别。该列位于“i”内static_cast.

顺便说一下,这里是remove_some_extents:

// Forward declaration
template < typename Array, std::size_t Count >
struct remove_some_extents;

// Case with indefinite array but no extents to strip
template < typename T >
struct remove_some_extents< T[], 0u >
{ typedef T type[]; };

// Case with definite array but no extents to strip
template < typename T, std::size_t N >
struct remove_some_extents< T[N], 0u >
{ typedef T type[N]; };

// Case with non-array type but no extents to strip
template < typename T >
struct remove_some_extents< T, 0u >
{ typedef T type; };

// Case with indefinite array and extents to strip
template < typename T, std::size_t L >
struct remove_some_extents< T[], L >
{ typedef typename remove_some_extents<T, L - 1u>::type type; };

// Case with definite array and extents to strip
template < typename T, std::size_t N, std::size_t L >
struct remove_some_extents< T[N], L >
{ typedef typename remove_some_extents<T, L - 1u>::type type; };

// Right now, non-array type with non-zero strip count should give an error.

Thanks.

Edit:添加了基本情况和右值重载checked_slice.

附录:我得到了一些有用的东西,但我不知道为什么旧方法不起作用。

我首先注释掉了 r 值过载checked_slice,以减少我必须处理的变量。然后我做了一个版本checked_slice这适用于标准容器,但您不需要看到它,因为它没有帮助。 (我将其注释掉以确保它不会产生影响。)

我将常规版本更改为:

template < typename E, typename T, std::size_t N, typename ...V >
inline constexpr
auto  checked_slice( E &&e, T (&t)[N], std::size_t u, V &&...v )
 -> typename remove_some_extents<T[N], 1u + sizeof...(V)>::type &
{
    return checked_slice( static_cast<E &&>(e), static_cast<T &>(t[ u ]),
     static_cast<V &&>(v)... );
}

即,我删除了实际测试和失败时抛出的部分,并且代码有效!问题似乎不是索引,而是抛出和/或条件!果然,当我把它改成:

template < typename E, typename T, std::size_t N, typename ...V >
inline
auto  checked_slice( E &&e, T (&t)[N], std::size_t u, V &&...v )
 -> typename remove_some_extents<T[N], 1u + sizeof...(V)>::type &
{
    if ( u < N )
        return checked_slice(static_cast<E &&>(e),t[u],static_cast<V &&>(v)...);
    else
        throw e;
}

它仍然有效!是什么让我认为可以使用 throw 语句作为条件表达式的操作部分之一?这是我的编译器中的错误吗?

我可能不得不放弃并将边界检查分离为纯函数并在失败时抛出at method.


自从我更改了这个问题以来,它可能还没有被标记为新问题。我有一个答案 https://stackoverflow.com/a/16247205/1010226从重新询问另一个帖子 https://stackoverflow.com/q/16246123/1010226,并且成功了。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

你能在条件表达式中添加条件表达式吗? (是:边界检查如何扩展到多个维度?) 的相关文章

  • cocos2d 2.0-rc2:结束director并重新启动

    我有一款由 cocos2d 驱动的游戏 它使用 UIKit 菜单 所以我只使用一个视图控制器的框架 即游戏本身 而且 它只有一个场景 从cocos2d 2 0开始 director本身就是一个UIViewController子类 所以我只是
  • 调用一个从 AngularJS 表达式本地计算值的函数是不是很糟糕?

    我读了关于使用范围的一些 AngularJS 陷阱的文章 http thenittygritty co angularjs pitfalls using scopes 并且它指出您不应在表达式中使用函数 并且我知道每次框架认为需要时都可能会
  • 禁用 ASPNET 身份 2.0 中的用户

    我正在寻找一种方法来禁用用户而不是从系统中删除它们 这是为了保持相关数据的数据完整性 但似乎 ASPNET 身份只提供删除帐户 有一个新的锁定功能 但似乎可以控制锁定以禁用用户 但只有在尝试了一定次数的错误密码后才将用户锁定 还有其他选择吗
  • 如何将函数导入到Vue组件中?

    我正在尝试将单个函数导入到我的 Vue 组件中 我为我的函数创建了一个单独的 js 文件 randomId js exports randomId gt My function 在我的 Vue 组件中 我导入了 Random js let
  • Pandas:数据帧累积和,如果其他列为假则重置[重复]

    这个问题在这里已经有答案了 我有一个包含 2 列的数据框 这里的目标很简单 如果行列设置为 False 则重置 df cumsum df value condition 0 1 1 1 2 1 2 3 1 3 4 0 4 5 1 想要的结果
  • 什么时候使用静态库需要头文件?

    如果我在 Linux 中用 C 创建一个静态库并生成 a 文件 我 或其他人 如何使用该库 例如 我的库定义了一个类 我认为仅仅提供 a 文件是不够的 还需要提供头文件 我如何知道 a 文件必须提供哪些头文件 例如 我是否需要提供我的库代码
  • 如何将 char 转换为 unsigned int?

    我有一个字符数组 它实际上用作字节数组 而不是用于存储文本 在数组中 有两个特定字节表示我需要存储到无符号 int 值中的数值 下面的代码解释了设置 char bytes bytes 2 bytes 0 0x0C For the sake
  • 我可以在 Android Market 上出售我的 SL4A 应用程序吗?

    Closed 这个问题是无关 help closed questions 目前不接受答案 我想使用 SL4A 在 Android 上使用 Python 开发一个应用程序 并且我想知道是否可以将其作为应用程序在 Android Market
  • 有什么方法可以禁用/覆盖 Galaxy Tab 4 上的多任务按钮吗? [复制]

    这个问题在这里已经有答案了 我们编写了一个工业控制应用程序 并随我们的制造产品预装在三星 Galaxy 平板电脑上 我们使用的平板电脑是运行 Honeycomb 的 Tab 10 但我们无法再获得足够的 OEM 平板电脑 因此我们改用运行
  • 如何使用 PHP 从 iframe 获取 url

    如何从下面的链接获取 YouTube 网址 您可以使用 regex 和 preg match 函数 preg match src iframe string match url match 1 UPDATE如果您有使用 php 生成的页面或
  • 当从 HDFS 手动删除分区数据时,如何更新 Hive 中的分区元数据

    自动更新Hive分区表元数据的方法是什么 如果新的分区数据被添加到HDFS 不执行alter table添加分区命令 然后我们可以通过执行命令 msck Repair 来同步元数据 如果从HDFS中删除了大量分区数据 没有执行alter t
  • 设置 Silex Bootstrap 时出现 Apache 错误:无法检查 htaccess 文件

    我正在尝试使用 Silex Bootstrap 建立一个网站 我已将它与其他 Web 项目一起放在我的文件夹中 并更改了 Apache 配置中的 DocumentRoot
  • 如何在 Xcode 10 中恢复快速帮助?

    在我升级到 Xcode 10 后 快速帮助信息仅提供所选类或结构的声明 是否有某个设置可以使其与 Xcode 9 中的设置相同 升级后我遇到了同样的问题 其中函数签名是单击选项时唯一显示的内容 当我删除里面的所有内容后 快速帮助再次出现 L
  • Docker-compose:npm 安装成功后卷中不存在 node_modules

    我有一个具有以下服务的应用程序 web 在端口 5000 上保存并运行 python 3 Flask Web 服务器 使用 sqlite3 worker 有一个index js文件是队列的工作人员 Web 服务器通过端口使用 json AP
  • 我可以以某种方式“编译”一个Python脚本以在没有安装Python的PC上运行吗?

    所以我有一个Python脚本 myscript py 我是这样执行的 python D myscript py 但是 我必须安装 Python 并将其包含在PATH使其工作的环境变量 是否有可能以某种方式将 Python 可执行文件与 Py
  • 如何向 SvelteKit/Vite 应用添加版本号?

    我正在尝试在我的 SvelteKit 应用程序中创建一个系统 它会在某个页面上向您显示有关当前应用程序版本的信息 最好是 Git 提交哈希和描述 我尝试使用Vite的定义功能 https vitejs dev config define在构
  • 如何解释 mgcv 的随机效应图

    我有一些关于在 GAM 中使用随机效果的问题 首先 您如何解释和传达输出图 我在这个 GAM 中将火灾建模为随机效应 因为它在我的不同现场站点上很大程度上是随机发生的 并且我只将其记录为二进制 它不能作为普通变量工作 因为它的级别太少 而且
  • Cakephp - CSRF 令牌不匹配

    我在 Cakephp 3 6 中有一个项目 其中 MessageController 中的 3 个操作由 Ajax 调用 但是 我有一个问题 当我向其中一个操作发送请求时 XHR 会向我返回以下内容 message CSRF token m
  • 首选项和操作栏中的开/关切换按钮 - 冰淇淋三明治风格

    我指的是 ICS 手机上默认 Android 设置应用程序中看到的蓝色开 关样式 也可以在这里看到 http android developers blogspot com 2012 02 android design v2 now wit
  • java中的回调是什么[重复]

    这个问题在这里已经有答案了 可能的重复 什么是回调函数 https stackoverflow com questions 824234 what is a callback function 我已经阅读了回调的维基百科定义 但我仍然没有明

随机推荐

  • Django持久数据库连接

    我将 django 与 apache mod wsgi 和 PostgreSQL 都在同一主机上 一起使用 并且我需要处理大量简单的动态页面请求 每秒数百个 我面临的问题是 瓶颈是 django 没有持久的数据库连接 并且在每个请求上重新连
  • lapply 和 mutate_all/for 循环

    我在列表中有几个数据框 我必须通过标准化所有列中的所有数据来修改这些数据框 基本上 将每行 列除以该列数的总和 使用 lapply 加载所有原始数据帧后 我想迭代所有列以执行此类操作 即 mutate df df my column df
  • 如何正确解析这种日期?

    我无法为我的日期找到正确且干净的工作解决方案 其格式如下 201406082159 6月8日 21 59 此处 我上次尝试的是这样的 SimpleDateFormat format2 new SimpleDateFormat YYYYMMD
  • 将宽变长,但重复特定列

    我有一个数据框 如下所示 df2 pd DataFrame pid 1 2 3 4 BP1Date 12 11 2016 12 21 2016 12 31 2026 np nan BP1di 21 24 25 np nan BP1sy 12
  • SIM900 GSM/GPRS 未获得正确的 AT+CREG?回答

    我使用的是带有 IComsat SIM900 GSM GPRS 扩展板的 Arduino UNO 使用以下教程 Arduino 实时 GPS 追踪器 http www samaria me uk 2011 12 arduino live g
  • org.xml.sax.SAXParseException;cvc-complex-type.2.4.c:匹配通配符严格,但找不到声明

    我在这里要做的就是让邮件发送器通过我的 Java 代码工作 我查看了类似的问题 并按照此处的建议删除了 spring 版本号cvc complex type 2 4 c 匹配通配符严格 但找不到元素 mvc annotation drive
  • Play框架2.5.0 Websockets示例[关闭]

    Closed 这个问题正在寻求书籍 工具 软件库等的推荐 不满足堆栈溢出指南 help closed questions 目前不接受答案 播放框架 2 5 0 Websockets 示例 在 play 2 5 0 websockets 代码
  • 创建新分支时,基础分支是什么?

    创建分支时我需要确认 纠正我的假设 如果我在主分支中 执行以下操作后 git checkout b some branch 这意味着我已经从 master 开始了一个新分支 另一方面 如果我签出另一个分支 并在那里创建一个分支 git ch
  • 将网络表单值返回至 Google 应用脚本

    我有一个谷歌表格脚本 允许用户输入某些数据 当用户单击 确定 时 我希望能够将这些值传递回谷歌应用程序脚本中的函数 这是我试图开始工作的 Google 表格脚本 函数 checkLogin 确实会被调用 直到我尝试从网页将值传递给它 剧本
  • 如何监控`preStop`命令的执行?

    我正在尝试使用 pod 的生命周期事件 问题是来自的命令preStop根本不运行 有什么办法可以监控它是否已启动吗 容器的日志为空 lifecycle preStop exec command bin sh c clean sh 我只想添加
  • 矩阵到 JTable

    我需要用静态二维数组填充 JTable 我创建了这个模型JTable public class InsertMatToJTable extends AbstractTableModel String titre age real sex r
  • 如何随机选择三个字符串之一?

    我必须创建一个密码 我相信我可以做到 但是我一开始就被难住了 我有3个字符串 我想随机选择这三个字符串之一 有人知道该怎么做吗 Dim sLowerCase As String qwertyuiopasdfghjklzxcvbnm Dim
  • 将文件拖放到 SharePoint Web 部件中以上传到 DocLibrary

    是否可以创建一个 SharePoint wss3 或 MOSS 2007 Web 部件 以允许将文件拖放到其上 然后将文件上传到预定义的文档库中 我想这需要某种形式的客户端脚本 Ajax 但我对 ajax 的了解有点粗略 从迄今为止的探索来
  • 已使用的 Objective-C 前缀列表

    我正在为我正在编写的库选择一个命名空间 并且希望避免与其他命名空间发生冲突 有谁知道有一个网站列出了所有正在使用的类前缀 https cocoadev github io ChooseYourOwnPrefix https cocoadev
  • FFMPEG 没有按预期“切割”

    我通过一个简单的 system process 使用 FFMPEG 来自 java 应用程序 并尝试将视频切成块 我正在尝试将其切成 10 秒的增量 我的 FFMPEG 命令如下所示 ffmpeg i SampleVideo mp4 ss
  • 在 Android BLE 中处理指示而不是通知

    使用蓝牙 SIG 应用加速器代码 它很好地演示了蓝牙低功耗的不同概念 然而 它没有提到与通知相反的指示 我知道与通知不同 需要确认指示 并且在代码中我会这样做byte val enabled BluetoothGattDescriptor
  • XMLHttpRequest:网络错误 0x80070005,在 Microsoft Edge(但不是 IE)上访问被拒绝

    我有一个非常简单的 ajax 请求 见下文 服务器正在使用 CORS 并且在 IE 10 Chrome Firefox 和 Opera 中运行良好 On 微软边缘但是 它失败了 XMLHttpRequest 网络错误 0x80070005
  • Linux 中 POSIX 可靠信号和 POSIX 实时信号有什么区别?

    我读了一个手册页signal using 男人7信号 http man7 org linux man pages man7 signal 7 html我看到两种类型的信号 所以 我有一个问题 有什么区别POSIX 可靠信号 and POSI
  • 单个 DLL V 多个 DLL

    前段时间我在这里问了一个问题 当时我想知道是否最好将一个大项目 NET 类库 拆分为多个 NET DLL 建议使用一个大的 DLL 该 DLL 现在已在另一个项目中使用 另一个项目只使用了几个类 因此项目中有很多类未使用 从体系结构的角度来
  • 你能在条件表达式中添加条件表达式吗? (是:边界检查如何扩展到多个维度?)

    Note 我通过实现一个完全不同的问题来解决最初的问题 有关新的实际问题 请参阅附录 但您可以阅读前一部分以了解上下文 这是我的一个的扩展以前的帖子 https stackoverflow com q 10171525 1010226 我根