"Francesco"
Its in the latest draft standard. Some libraries might have it within
the TR1 namespace.
Its out of date. Its quite well known that copy_if was omitted by the
standard.
What do you thing about this version, which allows copying a smaller
array to the beginning of a larger one and uses std::copy which is
surely part of the ISO STL?
-------
template <typename T, size_t Nfrom, size_t Nto>
inline bool copy(T (&from)[Nfrom], T (&to)[Nto]) {
if (Nto < Nfrom) {
return false;
} else {
std::copy(from, &from[Nfrom], to);
return true;
}
}
The OP has arrays of a single size. I don't think your function has any
use as it is. Nfrom and Nto are compile-time constants and you have a
run-time comparison of them. You have taken the address of a
non-existing element from[Nfrom] which is usually doable but isn't good.
You can write from + Nfrom.
Fraser.
Its in the latest draft standard. Some libraries might have it within
the TR1 namespace.
Thank you, I didn't knew that.
Its out of date. Its quite well known that copy_if was omitted by the
standard.
What do you thing about this version, which allows copying a smaller
array to the beginning of a larger one and uses std::copy which is
surely part of the ISO STL?
-------
template <typename T, size_t Nfrom, size_t Nto>
inline bool copy(T (&from)[Nfrom], T (&to)[Nto]) {
if (Nto < Nfrom) {
return false;
} else {
std::copy(from, &from[Nfrom], to);
return true;
}
}
The OP has arrays of a single size. I don't think your function has any
use as it is. Nfrom and Nto are compile-time constants and you have a
run-time comparison of them. You have taken the address of a
non-existing element from[Nfrom] which is usually doable but isn't good.
You can write from + Nfrom.
Fraser.
You're right, the different sized array assignment wasn't requested so
there is no need for my function in this particular case, although it
works just fine - that's just a more flexible version instead of a
more specialized one, as I see it.
Could you please explain me why it isn't good to use "&from[Nfrom]",
being that "from + Nfrom" is just exactly the same thing, and since
the standard allows me to take the address of the first element past
the array in either way? Am I mistaking the standard or is it just
matter of tastes?
About the runtime check, if I understand it right, a good compiler
should be able to optimize away that comparison and generate the
actual functions accordingly. On the other hand, if I omit that check,
my code could call that function with a bad couple of arrays and I
would end up writing outside the boundaries, instead of having a safe
do-nothing function which just returns false.
I've just tried the same function without that check, it just compiled
and it just wrote outside the boundaries as expected, when fed with
the wrong arrays. Is there a way to do such a check at compile time,
allowing the compiler to issue a warning or an error about it?
In any case, that's just matter of curiosity, I think I would never
manage actual data in arrays that way. Maybe it could be worth doing
in some low-level, hidden, optimized function, I don't know.
Please don't take my words in the wrong way, I'm not the kind of
person who likes to dive into polemics, I'm just willing to learn new
things, that's why I'm responding to your points that way.
Have good time,
Francesco