Skip to content

changed fplus::internal::get_index to reduce templated recursion and … #318

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ target_include_directories(
"$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>"
)

target_compile_features(fplus INTERFACE cxx_std_14)
target_compile_features(fplus INTERFACE cxx_std_17)

find_package(Threads REQUIRED)
target_link_libraries(fplus INTERFACE Threads::Threads)
Expand Down
3 changes: 1 addition & 2 deletions include/fplus/function_traits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ namespace utils {
R (C::*)(A...)>::type>::type type;
};
}

template <typename ReturnType, typename... Args>
struct function_traits<ReturnType(Args...)> {
/**
Expand Down Expand Up @@ -441,7 +440,7 @@ namespace internal {
};

template <typename ReturnType, typename ClassType, typename... Args>
struct has_function_traits<ReturnType (ClassType::*)(Args...)&> : std::true_type {
struct has_function_traits<ReturnType (ClassType::*)(Args...) &> : std::true_type {
};

template <typename ReturnType, typename ClassType, typename... Args>
Expand Down
28 changes: 13 additions & 15 deletions include/fplus/variant.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,23 +93,21 @@ namespace internal {
typedef std::shared_ptr<T> type;
};

// http://stackoverflow.com/a/27588263/1866775

template <typename T, typename... Ts>
struct get_index;

template <typename T, typename... Ts>
struct get_index<T, T, Ts...> : std::integral_constant<std::size_t, 0> {
template <typename What, typename... Ts>
struct contains {
/// True if T is in Ts...
constexpr static bool value = (std::is_same_v<What, Ts> || ...);
};

template <typename T, typename Tail, typename... Ts>
struct get_index<T, Tail, Ts...> : std::integral_constant<std::size_t, 1 + get_index<T, Ts...>::value> {
};

template <typename T>
struct get_index<T> {
// condition is always false, but should be dependant of T
static_assert(sizeof(T) == 0, "element not found");
template <typename What, typename... Ts>
struct get_index {
/// Index of type T in List.
static_assert(contains<What, Ts...>::value, "element is not in list");
constexpr static auto value = []() {
return ([]() {std::size_t i = 0;
(... && (!std::is_same_v<What, Ts> && ++i));
return i; }());
}();
};

template <typename T, typename... Ts>
Expand Down
64 changes: 31 additions & 33 deletions include_all_in_one/include/fplus/fplus.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -344,22 +344,6 @@ namespace utils {
: public function_traits<decltype(&T::operator())> {
};

namespace xx_impl {
template <typename C, typename R, typename... A>
struct memfn_type {
typedef typename std::conditional<
std::is_const<C>::value,
typename std::conditional<
std::is_volatile<C>::value,
R (C::*)(A...) const volatile,
R (C::*)(A...) const>::type,
typename std::conditional<
std::is_volatile<C>::value,
R (C::*)(A...) volatile,
R (C::*)(A...)>::type>::type type;
};
}

template <typename ReturnType, typename... Args>
struct function_traits<ReturnType(Args...)> {
/**
Expand Down Expand Up @@ -404,6 +388,22 @@ namespace utils {
};
};

namespace xx_impl {
template <typename C, typename R, typename... A>
struct memfn_type {
typedef typename std::conditional<
std::is_const<C>::value,
typename std::conditional<
std::is_volatile<C>::value,
R (C::*)(A...) const volatile,
R (C::*)(A...) const>::type,
typename std::conditional<
std::is_volatile<C>::value,
R (C::*)(A...) volatile,
R (C::*)(A...)>::type>::type type;
};
}

#if __cplusplus > 201510L

template <typename ReturnType, typename... Args>
Expand Down Expand Up @@ -700,7 +700,7 @@ namespace internal {
};

template <typename ReturnType, typename ClassType, typename... Args>
struct has_function_traits<ReturnType (ClassType::*)(Args...)&> : std::true_type {
struct has_function_traits<ReturnType (ClassType::*)(Args...) &> : std::true_type {
};

template <typename ReturnType, typename ClassType, typename... Args>
Expand Down Expand Up @@ -3295,7 +3295,7 @@ namespace internal {
std::size_t pos_;
};

#if defined(_MSC_VER) && _MSC_VER >= 1900
#if defined(_MSC_VER) && _MSC_VER >= 1900 && _MSC_VER < 1915
template <typename T, std::size_t N>
struct std::_Is_checked_helper<array_back_insert_iterator<T, N>>
: public true_type { // mark array_back_insert_iterator as checked
Expand Down Expand Up @@ -14329,23 +14329,21 @@ namespace internal {
typedef std::shared_ptr<T> type;
};

// http://stackoverflow.com/a/27588263/1866775

template <typename T, typename... Ts>
struct get_index;

template <typename T, typename... Ts>
struct get_index<T, T, Ts...> : std::integral_constant<std::size_t, 0> {
};

template <typename T, typename Tail, typename... Ts>
struct get_index<T, Tail, Ts...> : std::integral_constant<std::size_t, 1 + get_index<T, Ts...>::value> {
template <typename What, typename... Ts>
struct contains {
/// True if T is in Ts...
constexpr static bool value = (std::is_same_v<What, Ts> || ...);
};

template <typename T>
struct get_index<T> {
// condition is always false, but should be dependant of T
static_assert(sizeof(T) == 0, "element not found");
template <typename What, typename... Ts>
struct get_index {
/// Index of type T in List.
static_assert(contains<What, Ts...>::value, "element is not in list");
constexpr static auto value = []() {
return ([]() {std::size_t i = 0;
(... && (!std::is_same_v<What, Ts> && ++i));
return i; }());
}();
};

template <typename T, typename... Ts>
Expand Down
4 changes: 1 addition & 3 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ include(../cmake/warnings.cmake)
find_package(doctest 2.4.11 CONFIG REQUIRED)
# for doctest_discover_tests
include(doctest)

enable_testing()

set(
tests
show_versions
Expand Down Expand Up @@ -75,6 +73,6 @@ foreach (name IN LISTS tests)
FunctionalPlus::fplus
doctest::doctest
)
target_compile_features("${name}" PRIVATE cxx_std_14)
target_compile_features("${name}" PRIVATE cxx_std_17)
doctest_discover_tests("${name}")
endforeach ()
Loading