Type_traits - continuous memory

I have an interface that deals with any type of container. std::vector, std::arrayand even std::basic_string. The problem is that there is nothing that would prevent someone from passing a container that does not have limited memory.

The current solution that I have is deletethe interface that I want to prevent.

void dosoemthing(const std::list&)=delete;
void dosoemthing(const std::map&)=delete;

However, I would prefer if I could just add a static statement based on the type. This leads to my question. Do they have a type trait for containers that can be used to determine if its memory is continuous? I go through the documentation and have not found anything yet. I thought before marking this lost case, which I would check with team A.

+4
source share
1 answer

You can bring your own character, and then use static_assertto test it:

#include <type_traits>
#include <vector>
#include <array>

template<typename T>
struct has_contiguous_memory : std::false_type {};

template<typename T, typename U>
struct has_contiguous_memory<std::vector<T, U>> : std::true_type {};

template<typename T>
struct has_contiguous_memory<std::vector<bool, T>> : std::false_type {};

template<typename T, typename U, typename V>
struct has_contiguous_memory<std::basic_string<T, U, V>> : std::true_type {};

template<typename T, std::size_t N>
struct has_contiguous_memory<std::array<T, N>> : std::true_type {};

template<typename T>
struct has_contiguous_memory<T[]> : std::true_type {};

template<typename T, std::size_t N>
struct has_contiguous_memory<T[N]> : std::true_type {};
+5
source

Source: https://habr.com/ru/post/1611188/


All Articles