Prechádzať zdrojové kódy

refactor: improve SFINAE macro to allow auto

Sam Jaffe 2 rokov pred
rodič
commit
7e431cc9c8

+ 7 - 4
include/iterator/capture_iterator.h

@@ -31,16 +31,19 @@ public:
   capture_iterator(It it) : super_t(it) { cache_ = super_t::dereference(); }
 
   value_type const & dereference() const { return cache_; }
-  SFINAE(detail::has_increment_v<It>, void) increment() {
+
+  SFINAE(detail::has_increment_v<It>) void increment() {
     super_t::increment();
     cache_ = super_t::dereference();
   }
-  SFINAE(detail::has_decrement_v<It>, void) decrement() {
+
+  SFINAE(detail::has_decrement_v<It>) void decrement() {
     super_t::decrement();
     cache_ = super_t::dereference();
   }
-  SFINAE(detail::is_advanceable_iterator_v<It>, void)
-  advance(difference_type off) {
+
+  SFINAE(detail::is_advanceable_iterator_v<It>)
+  void advance(difference_type off) {
     super_t::advance(off);
     cache_ = super_t::dereference();
   }

+ 0 - 46
include/iterator/detail/default_constructible_function_proxy.h

@@ -1,46 +0,0 @@
-//
-//  default_constructible_function_proxy.h
-//  iterator
-//
-//  Created by Sam Jaffe on 4/1/23.
-//  Copyright © 2023 Sam Jaffe. All rights reserved.
-//
-
-#include <optional>
-
-#include <iterator/detail/macro.h>
-
-namespace iterator::detail {
-template <typename F, typename = void>
-class default_constructible_function_proxy {
-public:
-  default_constructible_function_proxy() = default;
-  default_constructible_function_proxy(F func) : func_(func) {}
-
-  template <typename... Args> decltype(auto) operator()(Args &&... args) const {
-    if (!func_) { throw; }
-    return std::invoke(*func_, FWD(args)...);
-  }
-
-private:
-  std::optional<F> func_;
-};
-
-template <typename F>
-class default_constructible_function_proxy<
-    F, std::enable_if_t<std::is_default_constructible_v<F>>> {
-
-public:
-  default_constructible_function_proxy() = default;
-  default_constructible_function_proxy(F func) : func_(func) {}
-
-  template <typename... Args> decltype(auto) operator()(Args &&... args) const {
-    return std::invoke(func_, FWD(args)...);
-  }
-
-private:
-  F func_;
-};
-}
-
-#include <iterator/detail/undef.h>

+ 1 - 2
include/iterator/detail/macro.h

@@ -14,8 +14,7 @@
 #define REQUIRES_T(trait, rval, ...) std::enable_if_t<trait, rval>
 #define REQUIRES(trait) typename = REQUIRES_T(trait, void)
 
-#define SFINAE(trait, rval)                                                    \
-  template <bool _ = true> REQUIRES_T(trait && _, rval)
+#define SFINAE(trait) template <bool _ = true, REQUIRES(trait && _)>
 
 #define VAL(X) std::declval<X>()
 #define DEREF_TYPE(X) decltype(*VAL(X))

+ 1 - 2
include/iterator/filter_iterator.h

@@ -9,7 +9,6 @@
 
 #include <functional>
 
-#include <iterator/detail/default_constructible_function_proxy.h>
 #include <iterator/detail/traits.h>
 #include <iterator/end_aware_iterator.h>
 #include <iterator/facade.h>
@@ -60,7 +59,7 @@ public:
   }
 
   end_aware_iterator<Iter> base_;
-  detail::default_constructible_function_proxy<Pred> pred_;
+  Pred pred_;
 };
 
 template <typename C, typename P>

+ 1 - 1
include/iterator/indexed_iterator.h

@@ -46,7 +46,7 @@ public:
   difference_type distance_to(indexed_iterator const & other) const {
     return other.base_ - base_;
   }
-  SFINAE(detail::has_sentinel_type_v<It>, bool) at_end() const {
+  SFINAE(detail::has_sentinel_type_v<It>) bool at_end() const {
     return base_ == typename It::sentinel_type();
   }
 

+ 4 - 4
include/iterator/proxy.h

@@ -22,7 +22,7 @@ public:
   decltype(auto) dereference() const { return *impl_; }
   void increment() { ++impl_; }
   bool equal_to(Self const & other) const { return impl_ == other.impl_; }
-  SFINAE(detail::has_sentinel_type_v<It>, bool) at_end() const {
+  SFINAE(detail::has_sentinel_type_v<It>) bool at_end() const {
     return impl() == typename It::sentinel_type();
   }
 
@@ -43,7 +43,7 @@ public:
   decltype(auto) dereference() const { return *impl_; }
   void increment() { ++impl_; }
   bool equal_to(Self const & other) const { return impl_ == other.impl_; }
-  SFINAE(detail::has_sentinel_type_v<It>, bool) at_end() const {
+  SFINAE(detail::has_sentinel_type_v<It>) bool at_end() const {
     return impl() == typename It::sentinel_type();
   }
 
@@ -65,7 +65,7 @@ public:
   void increment() { ++impl_; }
   void decrement() { --impl_; }
   bool equal_to(Self const & other) const { return impl_ == other.impl_; }
-  SFINAE(detail::has_sentinel_type_v<It>, bool) at_end() const {
+  SFINAE(detail::has_sentinel_type_v<It>) bool at_end() const {
     return impl() == typename It::sentinel_type();
   }
 
@@ -94,7 +94,7 @@ public:
   difference_type distance_to(Self const & other) const {
     return other.impl_ - impl_;
   }
-  SFINAE(detail::has_sentinel_type_v<It>, bool) at_end() const {
+  SFINAE(detail::has_sentinel_type_v<It>) bool at_end() const {
     return impl() == typename It::sentinel_type();
   }
 

+ 0 - 2
iterator.xcodeproj/project.pbxproj

@@ -70,7 +70,6 @@
 		CD5AEB3229D8886200A390A4 /* undef.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = undef.h; sourceTree = "<group>"; };
 		CD5AEB3329D8956600A390A4 /* capture_iterator.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = capture_iterator.h; sourceTree = "<group>"; };
 		CD5AEB3429D897DD00A390A4 /* capture_iterator_test.cxx */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = capture_iterator_test.cxx; sourceTree = "<group>"; };
-		CD8D0C7B29D8C0BF00443256 /* default_constructible_function_proxy.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = default_constructible_function_proxy.h; sourceTree = "<group>"; };
 		CDA2B60928581255004D5353 /* libiterator.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libiterator.a; sourceTree = BUILT_PRODUCTS_DIR; };
 		CDA2B6122858128C004D5353 /* forwards.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = forwards.h; sourceTree = "<group>"; };
 		CDA2B6132858128C004D5353 /* facade.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = facade.h; sourceTree = "<group>"; };
@@ -196,7 +195,6 @@
 			isa = PBXGroup;
 			children = (
 				CDA2B61A2858128C004D5353 /* arrow_proxy.h */,
-				CD8D0C7B29D8C0BF00443256 /* default_constructible_function_proxy.h */,
 				CD5AEAE829D86DCD00A390A4 /* facade_traits.h */,
 				CD5AEB3129D8885400A390A4 /* macro.h */,
 				CDA2B6182858128C004D5353 /* recursive_traits.h */,