This commit is contained in:
Erik Frojdh
2019-08-13 09:16:29 +02:00
parent c2f57f5ab0
commit 6f6ee19906
3 changed files with 25 additions and 3 deletions

View File

@ -31,7 +31,6 @@ template <class T, class Allocator = std::allocator<T>> class Result {
Result() = default; Result() = default;
Result(std::initializer_list<T> list) : vec(list){}; Result(std::initializer_list<T> list) : vec(list){};
/** Custom constructor from integer type to Result<ns> or Result<bool> */ /** Custom constructor from integer type to Result<ns> or Result<bool> */
template <typename V, typename = typename std::enable_if< template <typename V, typename = typename std::enable_if<
std::is_integral<V>::value && std::is_integral<V>::value &&
@ -98,9 +97,14 @@ template <class T, class Allocator = std::allocator<T>> class Result {
vec.push_back(std::forward<V>(value)); vec.push_back(std::forward<V>(value));
} }
/** Disable emplace_back if the underlying vector does not support it
* vector<bool> gcc 4.8
*/
template <typename... Args> template <typename... Args>
auto emplace_back(Args &&... args) -> decltype(vec.emplace_back(args...)){ auto emplace_back(Args &&... args) ->
vec.emplace_back(std::forward<Args>(args)...); typename std::enable_if<has_emplace_back<std::vector<T>>::value,
decltype(vec.emplace_back(args...))>::type {
return vec.emplace_back(std::forward<Args>(args)...);
} }
auto operator[](size_type pos) -> decltype(vec[pos]) { return vec[pos]; } auto operator[](size_type pos) -> decltype(vec[pos]) { return vec[pos]; }

View File

@ -35,6 +35,19 @@ struct has_str<T, typename std::conditional<
has_str_helper<decltype(std::declval<T>().str())>, has_str_helper<decltype(std::declval<T>().str())>,
void>::type> : public std::true_type {}; void>::type> : public std::true_type {};
/**
* Has emplace_back method
*/
template <typename T, typename _ = void> struct has_emplace_back : std::false_type {};
template <typename... Ts> struct has_emplace_back_helper {};
template <typename T>
struct has_emplace_back<T, typename std::conditional<
false,
has_emplace_back_helper<decltype(std::declval<T>().emplace_back())>,
void>::type> : public std::true_type {};
/** /**
* Type trait to evaluate if template parameter is * Type trait to evaluate if template parameter is
* complying with a standard container * complying with a standard container

View File

@ -43,4 +43,9 @@ TEST_CASE("sls::is_duration"){
TEST_CASE("initializer list"){ TEST_CASE("initializer list"){
REQUIRE(sls::is_light_container<std::initializer_list<int>>::value == true); REQUIRE(sls::is_light_container<std::initializer_list<int>>::value == true);
}
TEST_CASE("Check for emplace back"){
//we know vector should have this its the type trait that is tested
REQUIRE(sls::has_emplace_back<std::vector<int>>::value == true);
} }