I have code like this:
template<size_t N>
std::string_view concatenateString(std::array<char, N> &buffer, std::string_view s1, std::string_view s2){
// copy / concatenate string_view's into the buffer...
// check for buffer overflow etc...
return { buffer.data(), buffer.size() };
}
However, yesterday I forgot to pass buffer by reference:
std::string_view f(std::array<char, N> buffer, std::string_view s1, std::string_view s2){
// ^ missing &
return concatenateString(buffer, s1, s2);
}
Strangely enough because of the optimizer, code worked on some places and failed on other and I spent 15 min debugging it.
if std::array
was custom type, I would be able to delete copy c-tor and operator=, so errors like this can not happen.
However in the way it is, I do not see how this can be done, unless std::array
is wrapped in some other class, for example like this:
template<size_t N>
struct Buffer{
std::array<char, N> a;
};
However this will make code ugly, unless I recreate most of the methods. Is much easier to create my own buffer class with raw array inside.
Another possibility would be to derive new class from std::array
, which is even more ugly.
Do I miss any other possibility?