I fail to see how it could work. Those embedded systems with a few KB of ram? Which the garbage collector would sit eating all the time?
Originally Posted by zacs7
And then the problem of deterministic construction. Not saying such a thing isn't possible with GC, but it sure would made it a whole lot harder.
Locking pointers... Barking instructions at the GC...
I don't see how it's beneficial for embedded systems.
That's interesting. But if you follow the convention that all macros should be UPPER CASE, then there should be no problem, though?
Originally Posted by Mad_guy
I suspect it's because they want thing not to break if someone breaks it?
Ah, I get what you mean. But what you are trying to do is impossible. A vector is a dynamic containers. That is, its size can change at runtime. So there would be absolutely impossible for us to know the size of it at compile time. But we could use an assert:
Simple example: a vector type which is parametrized by its current length (i.e. the type is something abstractly like 'vector 5 int' meaning "this value is a vector of length 5 containing only ints".) You can then parametrize functions over these vectors and enforce invariants in your code. Example: addition of two vectors of length n
is basically undefined behavior if n != m
, or better put, addition is only defined precisely over two vectors of lengths n
where n == m
. You can express this invariant using the type system when defining a function like addition by saying it can only take two vectors where their types say they have the same length. The result is that if you add a vector of length 4 with a vector of length 5, you actually get a compile-time error because the invariant is enforced through the type system.
this can be done in C++ and if anybody would like to take a stab at it, I'd like to see the code, even if the result is only a trivially usable vector type! (and I'll respond with my own code doing the same thing, only in a much more high level language to show a contrast in type system expressiveness.) The result almost always results in the usage of Peano numbers at the type level, so if anybody would like to take a stab at it that's a good starting point.
assert(v1.size() == v2.size());
It's better than nothing.
Now, there are actually 3 solutions to this problem without using vectors.
The first is with standard C arrays:
The second is using std::array variant 1:
template<typename T, int N1, int N2> void AddVec(T (&Vec1)[N1], T (&Vec2)[N2])
static_assert(N1 == N2);
And the third is using std::array variant 2:
template<typename T, int N1, int N2> void AddVec(std::array<T, N1>& Vec1, std::array<T, N2>& Vec2)
static_assert(N1 == N2);
template<typename T> void AddVec(T & Vec1, T & Vec2)
static_assert(Vec1.size == Vec2.size);