I am implementing something very similar to std::vector
but uses array on the stack instead of memory allocation.
The d-tor calls a function that uses SFINAE.
- If
value_type
is POD the function have empty body. - If
value_type
is normal class suchstd::string
, the function have a body and destroy all the data properly.
Now, I want to be able to use this new std::vector
as constexpr
. However even the c-tor is declared constexpr
, the code does not compiles because the class have non trivial d-tor.
Here is small part of the code:
template<typename T, std::size_t SIZE>
class SmallVector{
constexpr SmallVector() = default;
~SmallVector(){
destructAll_<value_type>();
}
// ...
template<typename X>
typename std::enable_if<std::is_trivially_destructible<X>::value == true>::type
destructAll_() noexcept{
}
};
Is there anything I can do to make class be constexpr
if value_type
is POD and keeping functionality for non POD data types.
(Not at the same time of course)
SmallVectorImpl<T, SIZE, is_trivially_destructible<T>::value>
which will have a main definition and a specialization for trivially destructible types. The only difference between the two being the destructor. – Hackerstd::vector
can such a type be, really? Are you rather reimplementingstd::array
? Orrrr is it a vector with a huge auto-storage array and lots of placement new? :) – Cradling