Note: I solved the original problem by implementing a completely different one. See Appendix to a new topical issue, but you can read the previous part for context.
This is an extension of one of my previous posts . Based on this answer, I created a container class:
template < typename T, unsigned N0, unsigned ...N > struct array_md {
I am trying to make an at version for this container. I decided that I would just create a version of slice that accepts an exception object. Unlike generic slice , my checked_slice should accept an inline array object since pointers and class types (with operator [] ) don't have a (standard) way to give me evaluations.
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 ); }
( remove_some_extents does what it says, instead of one or all of the standard C ++ 11 library it gives you.) When I put this in my 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 )...); } //... };
I get errors related to matrix decay to pointer! (I am using TDC-GCC 4.7.1, which comes with CodeBlocks 12.11 for the 32-bit version of Windows-8 Pro.)
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]
I thought that using array references cancels the decay of the matrix into a pointer. Is this a GCC bug, or am I messing something up?
For "slice.hpp" line 141, gender 10 is the end of the instruction in the (l-value version of) checked_slice , and L142C1 is the final bracket of the function. In "array_md.hpp", L284C112 and L274C112 are the return (and only) operators of the functions at , const and non-const, respectively. The column is at "i" inside static_cast .
By the way, here is 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: Added overloads of base and r-values ββfor checked_slice .
Addendum: I have something that works, but I do not know why the old method did not work.
First, I commented on the r-value overload for checked_slice , in order to reduce the variables that I have to work with. Then I made a version of checked_slice that works for standard containers, but you do not need to see it because it did not help. (And I commented on this to make sure that this did not change the situation.)
I changed the regular version to:
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)... ); }
i.e. I removed the actual test and the failover parts and the code works! The problem, it seems, is not in indexing, but in the throw and / or conditional! Of course, when I changed it to:
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; }
It still works! Which gives, I thought, it would be nice to use the throw operator as part of the action of a conditional expression? Is this a bug in my compiler?
I may have to refuse and separate the border check with a clean function and a throw-fail in the at method.