fix slice copying

cpp
Avril 4 years ago
parent 150ab70dc1
commit 4432e50ebc
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -51,7 +51,7 @@ struct Span {
template<typename T> template<typename T>
struct Span<T>::Slice : public Span<T> { struct Span<T>::Slice : public Span<T> {
inline Slice(T* ptr, size_t sz) : _area((void*)ptr), _size(sz){} inline Slice(T* ptr, size_t sz) : _area((void*)ptr), _size(sz){}
inline Slice(const Span<T>& slice) : _area(slice.area()), _size(slice.size()){} inline Slice(const Span<T>& slice) : _area(const_cast<void*>(slice.area())), _size(slice.size()){}
inline Slice(const Slice& copy) = default; inline Slice(const Slice& copy) = default;
inline Slice(Slice&& copy) : _area(copy._area), _size(copy._size){ *const_cast<size_t*>(&copy._size) = 0; } inline Slice(Slice&& copy) : _area(copy._area), _size(copy._size){ *const_cast<size_t*>(&copy._size) = 0; }
Slice() = delete; Slice() = delete;
@ -65,5 +65,6 @@ struct Span<T>::Slice : public Span<T> {
const size_t _size; const size_t _size;
}; };
template<typename T> template<typename T>
typename Span<T>::Slice Slice; using Slice = Span<T>::Slice;

@ -3,6 +3,11 @@
#include <cstdio> #include <cstdio>
#include <cstring> #include <cstring>
void print_slice(Slice<unsigned char> memory)
{
printf("slice: { %p, %lu }\n", memory.area(), memory.size());
}
void write_fake(Cow& clone, const char* string) void write_fake(Cow& clone, const char* string)
{ {
strncpy(clone.area_as<char>(), string, clone.size_as<char>()-1); strncpy(clone.area_as<char>(), string, clone.size_as<char>()-1);
@ -17,6 +22,9 @@ int main()
{ {
Cow real(4096); Cow real(4096);
printf("Created real: ");
print_slice(real);
write_fake(real, "Hello world"); write_fake(real, "Hello world");
read_fake(real); read_fake(real);

Loading…
Cancel
Save