mirror of
https://github.com/ToruNiina/toml11.git
synced 2025-09-17 17:58:09 +08:00
feat: remove resize and add try_reserve
This commit is contained in:
@@ -36,30 +36,25 @@ inline std::unique_ptr<T> make_unique(Ts&& ... args)
|
|||||||
|
|
||||||
namespace detail
|
namespace detail
|
||||||
{
|
{
|
||||||
|
template<typename Container>
|
||||||
template<typename T>
|
void try_reserve_impl(Container& container, std::size_t N, std::true_type)
|
||||||
inline void resize_impl(T& container, std::size_t N, std::true_type)
|
|
||||||
{
|
{
|
||||||
container.resize(N);
|
container.reserve(N);
|
||||||
return ;
|
return;
|
||||||
}
|
}
|
||||||
|
template<typename Container>
|
||||||
template<typename T>
|
void try_reserve_impl(Container&, std::size_t, std::false_type) noexcept
|
||||||
inline void resize_impl(T& container, std::size_t N, std::false_type)
|
|
||||||
{
|
{
|
||||||
if(container.size() >= N) {return;}
|
return;
|
||||||
|
|
||||||
throw std::invalid_argument("not resizable type");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // detail
|
} // detail
|
||||||
|
|
||||||
template<typename T>
|
template<typename Container>
|
||||||
inline void resize(T& container, std::size_t N)
|
void try_reserve(Container& container, std::size_t N)
|
||||||
{
|
{
|
||||||
if(container.size() == N) {return;}
|
if(N <= container.size()) {return;}
|
||||||
|
detail::try_reserve_impl(container, N, detail::has_reserve_method<Container>{});
|
||||||
return detail::resize_impl(container, N, detail::has_resize_method<T>());
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace detail
|
namespace detail
|
||||||
|
Reference in New Issue
Block a user