Do not store const& to the function/functor by copy by value

However, one cannot store a function type directly, but only *pointers* to
function types. So I used Boost MPL to discriminate between function types
and other callable (such as lambdas or functors, that can be copied).
This commit is contained in:
Laurent Rineau 2018-04-18 20:08:13 +02:00
parent 301181ca53
commit cd032f05fc
1 changed files with 8 additions and 3 deletions

View File

@ -43,6 +43,8 @@
#include <boost/dynamic_bitset.hpp> #include <boost/dynamic_bitset.hpp>
#include <boost/type_traits/remove_reference.hpp> #include <boost/type_traits/remove_reference.hpp>
#include <boost/type_traits/remove_cv.hpp> #include <boost/type_traits/remove_cv.hpp>
#include <boost/type_traits/is_function.hpp>
#include <boost/mpl/if.hpp>
#include <CGAL/config.h> #include <CGAL/config.h>
#include <CGAL/assertions.h> #include <CGAL/assertions.h>
@ -66,19 +68,22 @@ public:
/// Constructor /// Constructor
Implicit_to_labeling_function_wrapper(const Function_& f) Implicit_to_labeling_function_wrapper(const Function_& f)
: r_f_(f) {} : f_(f) {}
// Default copy constructor, assignment operator, and destructor are ok // Default copy constructor, assignment operator, and destructor are ok
/// Operator () /// Operator ()
return_type operator()(const Point_3& p) const return_type operator()(const Point_3& p) const
{ {
return ( (r_f_(p)<0) ? 1 : 0 ); return ( (f_(p)<0) ? 1 : 0 );
} }
private: private:
typedef typename boost::mpl::if_<boost::is_function<Function_>,
Function_*,
Function_>::type Stored_function;
/// Function to wrap /// Function to wrap
const Function_& r_f_; Stored_function f_;
}; // end class Implicit_to_labeling_function_wrapper }; // end class Implicit_to_labeling_function_wrapper