So, I've been looking at boost::array but it does require default constructor defined. I think the best way of filling this array with data, would be through a push_back(const T&) method. Calling it more times than SIZE (known at compile-time) would result in assert or exception, depending on build configuration. This way it would always contain meaningful data. Does anyone know efficient, portable, reliable implementation of this concept?
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
- What uses more memory in c++? An 2 ints or 2 funct
相关文章
- Numpy matrix of coordinates
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Java call constructor from constructor
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
may be store a boost::variant in your boost::array? make the first parameter an int or something..
i.e.
okay you have to deal with a variant, but it's stack allocated...
Why does it have to reside on the stack? Do you have empirical evidence that creating and
reserve
ing avector
is too slow (using avector
seems like the obvious answer)?Even if it is, you can create a pool of vectors that have space reserved and
swap
one of the pre-allocated vectors into a local copy. When you're done with the local one, swap it back again (much like thesplice
trick forlist
s).boost::array<T, 12> ta;
is no different fromT[12] ta;
; if you don't use an initializer list then the elements will be default constructed.The common workaround would be
boost::array<T*, 12> ta;
or maybeboost::array<unique_ptr<T>, 12> ta;
.The only way to store by value is to copy, no way around that... This is what initializer lists do:
This outputs:
http://codepad.org/vJgeQWk5
Well, I would have thought that someone would have brought the answer now, however it seems not, so let's go.
What you are wishing for is something I have myself dreamed of: a
boost::optional_array<T,N>
.There are two variants:
boost::array< boost::optional<T>, N >
, that is each element may or may not be set.std::vector<T>
(somehow), that is all beginning elements are set and all following ones are not.Given the previous questions / comments, it seems you would like the second, but it doesn't really matter as both are quite alike.
Let's focus on those very special operations:
As you can notice, the main difficulty is to remember that:
There are many operations on traditional STL container that may be tricky to implement. On a
vector
, element shuffling (due toinsert
orerase
) are perhaps the most stricking examples.Also note that with C++0x and initializer-lists
vector
getemplace_back
to directly construct an element in place, thus lifting theCopyConstructible
requirement, might be a nice boon dependent on your case.In C++0x you got
std::array<type, size>
(probably the same as boost::array). You can initialize array data by usingfill()
orstd::fill_n()
:If you want to get it default-initialized at definition you can use copy-ctor: