How to "distinguish" a function with two arguments in a function with one argument?

In Matlab you can write:

S = @(x,y) x^2+y^2-1
G = @(x) S(x,1);

If I have a function that expects a function with one argument, I can do it above. How to do this in c / c ++?

I have a library function (from the CGAL library) that expects as an argument a function that itself has only one argument. Ideally, I have a class ( SphericalHarmonics), and I would like to have a member function that takes one argument. Therefore, I have:

FT SphericalHarmonics::distFunction(Point_3 p)

(note that FTis a type similar to double), but of course when I try

SphericalHarmonics *sh = new SphericalHarmonics(1);
Surface_3 surface(sh->distFunction, Sphere(ORIGIN,2.));

thisalso considered as an argument, my function distFunctionis a function with two arguments, and an error occurs.

Please note that this can be solved using global variables, i.e.

SphericalHarmonics *sh;
FT dist_function(Point_3 p) {
    return sh->distFunction(p);
}

main() {
    sh = new SphericalHarmonics(1);
    Surface_3 surface(dist_function);
}

. , , CGAL. ​​

!

[]

@Andy-Prowl: std::bind lambda, , , .

main :

SphericalHarmonics *sh = new SphericalHarmonics(cInit, numL, symm);
auto fxn = std::bind(&SphericalHarmonics::distFunction, sh, std::placeholders::_1);
Surface_3 surface(fxn, Sphere_3(ORIGIN,2.));

:

~/lib/basisfunctions/SphericalHarmonics2/mesh_an_implicit_function.cpp:62:48: 
error: no matching function for call to     
‘CGAL::Implicit_surface_3<CGAL::Robust_circumcenter_traits_3<CGAL::Epick>, 
double (*)
(CGAL::Point_3<CGAL::Epick>)>::Implicit_surface_3(std::_Bind<std::_Mem_fn
<double (SphericalHarmonics::*)(CGAL::Point_3<CGAL::Epick>)>
(SphericalHarmonics*, std::_Placeholder<1>)>&, Sphere_3)’

~/CGAL-4.1/include/CGAL/Implicit_surface_3.h:50:5: note:   no known conversion 
for argument 1 from ‘std::_Bind<std::_Mem_fn<double (SphericalHarmonics::*)
(CGAL::Point_3<CGAL::Epick>)>(SphericalHarmonics*, std::_Placeholder<1>)>’ to 
‘CGAL::Implicit_surface_3<CGAL::Robust_circumcenter_traits_3<CGAL::Epick>, 
double (*)(CGAL::Point_3<CGAL::Epick>)>::Function 
{aka double (*)(CGAL::Point_3<CGAL::Epick>)}’

~/CGAL-4.1/include/CGAL/Implicit_surface_3.h:34:9: note:   
candidate expects 1 argument, 2 provided

[]

, , (.. surface ). std::bind. , , , ( ). , Andy-Prowl , , , .

+5
3

1:

, - (, , this), static:

class SphericalHarmonics
{
    ...
    static double distFunction(Point p);
    ...
};

double SphericalHarmonics::distFunction(Point p)
{
    ...
}

:

surface(SphericalHarmonics::distFunction);

2:

std::bind() curry-- distFunction , ( ++ 11, boost::bind() Boost.Bind):

#include <functional>

SphericalHarmonics *sh = new SphericalHarmonics(1);
auto fxn = std::bind(&SphericalHarmonics::distFunction, sh, _1);
surface(fxn);

3:

, ++ 11 :

SphericalHarmonics *sh = new SphericalHarmonics(1);
auto fxn = [=] (double d) { return sh->distFunction(d); } 
+5

std:: bind boost:: bind:

#include <functional>   

SphericalHarmonics *sh = new SphericalHarmonics(1);
surface(std::bind(&SphericalHarmonics::distFunction, sh, _1));
+5

CGAL Surface_3. , - ( ) Surface_3:

typedef CGAL::Surface_mesh_default_triangulation_3 Tr;
// c2t3
typedef CGAL::Complex_2_in_triangulation_3<Tr> C2t3;
typedef Tr::Geom_traits GT;
typedef GT::Sphere_3 Sphere_3;
typedef GT::Point_3 Point_3;
typedef GT::FT FT;
typedef FT (*Function)(Point_3);
typedef CGAL::Implicit_surface_3<GT, Function> Surface_3;

This is misleading because it looks like the isosurface CGAL classes can only deal with function pointers (as opposed to std::function, etc.). But the problem is that we just defined it that way. Just determine Surface_3to use std::function<FT (Point_3)>your template as an argument, std::bindand the lambdas from Andy Prowl 's answer will work just fine:

...
typedef std::function<FT (Point_3)> Function;
typedef CGAL::Implicit_surface_3<GT, Function> Surface_3;
+1
source

All Articles