Workaround lack of lvalue/rvalue operator overload on MSVC 2013.

This commit is contained in:
Hans-Kristian Arntzen 2019-04-09 16:20:33 +02:00
parent bbac2e8d8d
commit 3c3d4d74f7
2 changed files with 13 additions and 1 deletions

View File

@ -156,16 +156,25 @@ public:
}
// Makes it easier to consume SmallVector.
#if defined(_MSC_VER) && _MSC_VER < 1900
explicit operator std::vector<T>() const
{
// Another MSVC 2013 workaround. It does not understand lvalue/rvalue qualified operations.
return std::vector<T>(ptr, ptr + buffer_size);
}
#else
// Makes it easier to consume SmallVector.
explicit operator std::vector<T>() const &
{
return std::vector<T>(ptr, ptr + buffer_size);
}
// If we are converting as an r-value, we can pilfer our elements.
explicit operator std::vector<T>() const &&
explicit operator std::vector<T>() &&
{
return std::vector<T>(std::make_move_iterator(ptr), std::make_move_iterator(ptr + buffer_size));
}
#endif
// Avoid sliced copies. Base class should only be read as a reference.
VectorView(const VectorView &) = delete;

View File

@ -197,11 +197,14 @@ static void convert_to_std_vector()
SPVC_ASSERT(ints[0].v == 1);
SPVC_ASSERT(ints[1].v == 2);
// This doesn't work on MSVC 2013. Ignore it.
#if !(defined(_MSC_VER) && _MSC_VER < 1900)
SmallVector<std::unique_ptr<RAIIInt>> move_only_buffer;
move_only_buffer.emplace_back(new RAIIInt(40));
std::vector<std::unique_ptr<RAIIInt>> move_only_vector(std::move(move_only_buffer));
SPVC_ASSERT(move_only_vector.size() == 1);
SPVC_ASSERT(move_only_vector[0]->v == 40);
#endif
}
int main()