2013-03-27 72 views
1

Q1)鑑於我們已C++ shared_ptr與std :: function或double(* SOME_NAME)(double);

typedef double (*function_t)(double);

typedef std::function<double(double)> function_t;

我們如何定義

std::shared_ptr<function_t> ptr_func = ???

一些功能。

比方說,我們想要一個shared_ptr到一個已經定義的對象(忘記了它現在失敗的目的 - 但我需要這個)。作爲示例,請考慮以下對於double類型的對象。

struct null_deleter 
{ 
    void operator() (void const *) const {}; 
}; 

int main() 
{ 
    // define object 
    double b=0; 

    // define shared pointer 
    std::shared_ptr<double> ptr_store; 
    ptr_store.reset(&b,null_deleter()); // this works and behaves how you would expect 
} 

雖然如果我們做function_t類型的shared_ptr相同,即

double f (double x) 
{ 
    // some_function + return 
} 

int main() 
{ 

    // define shared pointer 
    std::shared_ptr<function_t> ptr_store; 
    ptr_store.reset(&f,null_deleter()); // ERROR: does not work 
} 

返回的錯誤是像下面這樣:

error: cannot convert 'double (*)(double)' to 'double (**)(double)' in initialization 

或在std::function的情況下

error: cannot convert 'double (*)(double)' to 'std::function<double(double)>*' in initialization 

很顯然不是正確的類型過去了,可能是我們不允許這樣做

注(?):與g++4-7-2版本-std=c++11 -O3在Windows編譯MinGW的

的幾個答案做工精良,今天我會回覆你。這是早上6點30分,我還沒有去睡覺。我正在執行的整個設計模式可能是錯誤的,希望稍後我可以解釋一下:|

+0

可能工作的一種方法是std :: bind(&f)。 – 2013-03-27 05:02:21

+0

@Guy這不起作用,或者我得到一個錯誤'失配類型'_Tp1 *'和'std :: _ Bind(double(*())(double))''。但是我會看到我能用'std :: bind'做什麼,之前沒有用過。 – woosah 2013-03-27 05:09:00

+0

我爲你做了一些例子。不知道這是否正是你想要的(或爲什麼你想要它),但它顯示了這些類如何一起玩。 – 2013-03-27 05:15:14

回答

0

這是做作,但我能想出不理解你實際上試圖做的正是最好的:

#include <iostream> 

typedef std::function<double(double)> function_t; 

double f(double t) 
{ 
    return t*5; 
} 

int main(int argc, char*argv[]) 
{ 
    // You usually want to attach the shared pointer to a dynamically allocated object 
    std::shared_ptr<function_t> ptr; 
    ptr.reset(new function_t(std::bind(&f, std::placeholders::_1))); 
    std::cout << (*ptr)(5.0); 

    // But we can attach the smart pointer to a local function object on the stack (BAD IDEA) 
    function_t fff = std::bind(&f, std::placeholders::_1); 
    ptr.reset(&fff); 
    std::cout << (*ptr)(5.0); 
} 
0

我儘量保持清晰原始函數指針,而寧可包他們在一個函數對象。遵循這種方法,下面的代碼工作。該shared_ptr要包含一個指向給定作爲其第一個模板參數類型的對象,因此,如果您使用

std::shared_ptr<function_t> 

那麼它必須包含一個

function_t* 

你有那部分權利。

通常情況下,人們會在一個指針傳遞給一個動態分配的對象,如

std::shared_ptr<function_t> ptr_store(new function_t(f)); 

,但在你的情況,你想傳遞的對象是已經在堆棧上的地址。

因此,我創建了一個名爲foo的本地function_t對象,然後將其地址 - 與null_deleter一起傳遞給shared_ptr,並且一切正常。

我希望這有助於!

P.S.我用微軟的Visual Studio 2010

#include <functional> 
#include <memory> 
#include <iostream> 

typedef std::function<double(double)> function_t; 

struct null_deleter 
{ 
    void operator() (void const *) const {}; 
}; 

double f (double x) 
{ 
    return 2.0 * x; 
} 

int main(int argc, char* argv[]) 
{ 
    // A function object on the stack 
    function_t foo(f); 

    // Putting this in a local scope to verify that when the shared_ptr goes 
    // out of scope, nothing bad happens. 
    double result1; 
    { 
    std::shared_ptr<function_t> ptr_store; 
    ptr_store.reset(&foo, null_deleter()); 

    result1 = (*ptr_store)(10.0); 
    std::cout << result1 << std::endl; 
    } 
    // ptr_store has now been destroyed 

    // Calling the foo function object again just to be sure it's still OK 
    double result2 = foo(5.0); 
    std::cout << result2 << std::endl; 
} 
相關問題