On Tuesday, October 1, 2013 1:42:02 PM UTC+2, James Kanze wrote:
>
> Except that std::array doesn't replace it in its main uses. You
> can't pass an `std::array` to a C interface (you need to take
> the address of it's first element explicitly), and you can't get
> the compiler to calculate its dimensions from the number of
> initializers.
The latter is easy enough. However, the most natural notation for that, exemplified by the CPPX_ARRAY_VALUES macro below, causes an internal compiler error with Visual C++ 12 RC. The CPPX_DECLARE_ARRAY macro works with both Visual C++ and g++.
Disclaimer: I just wrote this code off the cuff (with the PP macro stuff mostly lifted from an old posting in comp.lang.c++), so it's not tested more than compiling it and noting that it produces the expected output.
Point: one can easily obtain array dimensions from initalizers, at the cost of using macros. :-)
[code]
#include <algorithm>
#include <array>
#include <initializer_list>
#include <stddef.h>
#include <tuple>
#include <typeinfo>
typedef ptrdiff_t Size;
#define CPPX_INVOKE( macro, args ) macro args
#define PP_ARG_N( \
_1, _2, _3, _4, _5, _6, _7, _8, _9,_10, \
_11,_12,_13,_14,_15,_16,_17,_18,_19,_20, \
_21,_22,_23,_24,_25,_26,_27,_28,_29,_30, \
_31,_32,_33,_34,_35,_36,_37,_38,_39,_40, \
_41,_42,_43,_44,_45,_46,_47,_48,_49,_50, \
_51,_52,_53,_54,_55,_56,_57,_58,_59,_60, \
_61,_62,_63,N,...) N
#define PP_RSEQ_N() \
63,62,61,60, \
59,58,57,56,55,54,53,52,51,50, \
49,48,47,46,45,44,43,42,41,40, \
39,38,37,36,35,34,33,32,31,30, \
29,28,27,26,25,24,23,22,21,20, \
19,18,17,16,15,14,13,12,11,10, \
9,8,7,6,5,4,3,2,1,0
#define PP_NARG_AUX( ... ) CPPX_INVOKE( PP_ARG_N, (__VA_ARGS__) )
#define PP_NARG( ... ) PP_NARG_AUX( __VA_ARGS__, PP_RSEQ_N() )
#define CPPX_DECLARE_ARRAY( name, first_value, ... ) \
std::array<decltype(first_value), 1 + PP_NARG( __VA_ARGS__ )> name = \
{ first_value, __VA_ARGS__ };
namespace cppx {
using std::array;
using std::copy;
using std::initializer_list;
template< Size n, class Item >
auto make_array( initializer_list<Item> values )
-> array<Item, n>
{
array<Item, n> result;
copy( values.begin(), values.end(), result.data() );
return result;
}
} // namespace cppx
#define CPPX_ARRAY_VALUES( first_value, ... ) \
cppx::make_array<1 + PP_NARG( __VA_ARGS__ )>( {first_value, __VA_ARGS__} )
#include <iostream>
auto main()
-> int
{
using namespace std;
CPPX_DECLARE_ARRAY( a, 1, 2, 3, 42 );
for( auto const v : a ) { cout << v << ' '; }
cout << endl;
auto const b = CPPX_ARRAY_VALUES( 10, 20, 30, 42 );
for( auto const v : b ) { cout << v << ' '; }
cout << endl;
}
[/code]
Now I noticed that Google has f**ked up their GG Usenet interface again, with the hierarchical view gone (possibly reflecting earlier bug or sabotage where they arbitrarily dropped references) and with quoted text double-spaced. I have fixed the double-spacing manually in this edit field, but when posting via Google Groups, who knows what the end result will be... :-( So, my apologies for formatting etc., in advance, however it turns out!
Cheers & hth.,
- Alf
PS: Where can one go these days for discussions with competent C++ folks? SO = Herb Schildt + troll zone, Usenet = spammy + also otherwise fouled up by Google Groups. So where? I think the technical content here should attest, to those who don't recognize me, that I'm not trolling. It's a genuine question: where?