2017-11-21 4 views
-1

나는 다음과 같은 유형을받는 여러 가지 기능을 가지고 :C++ : - 표준 : : 바인딩> 표준 : 기능

array2D 사용자 정의 유형이다
function<double(int,int,array2D<vector<double *>>*)> 

. 또한, 내가 인수로 다음받는 함수가 : 올바른 서명이 펑터를 첫 번째 값, temp을 결합, 반환하기 위해, 지금

double ising_step_distribution(double temp,int i,int j,array2D<vector<double *>>* model) 

을, 나는 쓰고 :

double temp = some_value; 
function<double(int,int,array2D<vector<double *>>*)> step_func = 
    [temp](int i, int j, array2D<vector<double *>>* model){ 
     return ising_step_distribution(temp,i,j,model); 
    } 
} 

그리고이 방법이 효과적입니다. 그러나 다음 휴식 : 다음 오류

auto step_func = 
    [temp](int i, int j, array2D<vector<double *>>* model){ 
     return ising_step_distribution(temp,i,j,model); 
    } 
} 

는 :

candidate template ignored: 
could not match 
'function<double (int, int, array2D<vector<type-parameter-0-0 *, allocator<type-parameter-0-0 *> > > *)>' 
against 
'(lambda at /Users/cdonlan/home/mcmc/main.cpp:200:25)' 
void mix_2D_model(function<double(int,int,array2D<vector<T*>>*)> step_distribution_func,... 

그래서, 코드 덩어리가, 추한 obfuscative 반복적 인

는 (나는이 많이 제작하고 있기 때문에).


내가 설명서를 읽고 있고, 내가 쓸 수 있어야 이해 :

function<double(int,int,array2D<vector<double *>>*)> step_func = 
    bind(ising_step_distribution,temp,_1,_2,_3); 

그러나 내가 본 한 예는 유형 function<void()>의 기능입니다. 이 오류는 다음 오류로 인해 실패합니다.

// cannot cast a bind of type 
// double(&)(double,int,int,array2D<vector<double *>>*) 
// as function<double(int,int,...) 

시각적으로 깨끗한 바인딩 및 캐스트를 얻으려면 어떻게해야합니까?

+1

두 번째 람다 무엇 에러가 발생합니까? 람다는 거의 항상'std :: bind'에 우선합니다; 가능한 경우 람다 (lambda) 버전을 사용하도록 노력할 것입니다. – 0x5453

+0

@ 0x5453 아, 그래. 1 초, 다시 실행합니다. – bordeo

+0

@ 0x5453 오류가 발생했습니다. 'array2D > *' – bordeo

답변

3

How do I get a visually clean bind and cast?

한 가지 방법은 다음과 같습니다 다음

using F = function<double(int,int,array2D<vector<double *>>*)>; 
auto step_func = 
    [temp](int i, int j, array2D<vector<double *>>* model){ 
     return ising_step_distribution(temp,i,j,model); 
    } 
} 

그리고 :

auto step_func_2 = F(step_func); 
mix_2D_model(step_func_2, ...); 

또는 :

mix_2D_model(F(step_func), ...); 
+0

awesome - 시각적으로 보았습니다. 여기서 무슨 일을하는지 설명 할 시간이 있습니까?어느 쪽이든, 감사합니다 - 수락 – bordeo

+0

@bordeo 그것은''기능적 캐스트 표현식 '(http://en.cppreference.com/w/cpp/language/explicit_cast) 표기법을 사용합니다. 기본적으로'F' 생성자가 람다를 전달합니다. –

+0

좋아, 시원한 - 나는 그것을 조사 할 것이다. 고마워. – bordeo