Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 11 years ago.
Improve this questionI开发者_JAVA技巧n the spirit of Go-language, where simpler syntax is considered pretty important, here's a proposal for simpler array declaration in C++:
int value;
int_1 list;
int_2 table;
int_3 cube;
RECT rect;
RECT_1 rects;
Using typedefs this can expand to:
int value;
vector<int> list;
vector<vector<int> > table;
vector<vector<vector<int> > > cube;
RECT rect;
vector<RECT> rects;
Would you use it, or is there such a thing as too simple syntax?
EDIT:
there was a mistake in expansion syntax.. fixed vector< int>
to vector< vector< int> >
..
Names carry semantics, nothing else does. int_1
doesn't convey a lot in terms of meaning, whereas vector< int >
is quite clear here.
I understand that a simpler syntax is welcome, specifically in C++ where control characters (dots, brackets, parenthesis, semi-columns) often introduce graphical noise in the code.
But why use numbers in order to identify an array? Is this the array size? In this case, a vector<int>
might not be appropriate...
I would go with something simpler like intArray
. But again, you have to balance between the benefits and the WTF-effect when a programmer will see this.
EDIT: Wow, based on your edit, looks like the number means the dimension. It might be a little trickier than simply using a typedef since multi-dimensions array have difficult semantics. You to have initialize your array dimension by dimension whenever needed.
When is actually the hardest part, it might be simpler to initialize everything at the beginning but large data may force you to initialize dimensions as you go.
I wouldn't.
How is this simpler exactly?
Also, the syntax for arrays in C++ is:
int value;
int list[1];
int table[2];
int cube[3];
RECT rect;
RECT rects[1];
Not sure it that was the intention of your blurb since I don't see the sense in a 1 cell array.
I personally wouldn't use it. Maybe if you did macro's that would expand to tuples instead:
_(int, int) int_pair; // tr1::tuple<int, int>
_(int, int, int) coord_3d; // tr1::tuple<int, int, int>
If you don't like _
as a macro then you might want to look at using something less often used.
The choice of vector for an array would seem somewhat strange. Why not std::(tr1::)array
?
But how about something like that:
#include <vector>
template <class T, size_t N>
struct Nested
{
typedef std::vector< typename Nested<T, N - 1>::type > type;
};
template <class T>
struct Nested<T, 0u>
{
typedef T type;
};
int main()
{
Nested<int, 2>::type table;
table.push_back(Nested<int, 1>::type());
table.front().push_back(10);
}
For the lack of template typedefs until C++0x where you could add:
template <class T, size_t N>
using NestedVector = typename Nested<T, N>::type
//usage:
NestedVector<int, 3> vec; //vector<vector<vector<int>>>
精彩评论