2015-11-02 39 views
1

在函數中我需要傳遞一個可調用的參數std :: bind。 什麼是我應該使用的正確類型/模板?然後哪些類型用於callable to std :: bind

void foo(std::function<void(KnownType)> function, WhatShouldThisBe target) 
{ 
    std::bind(function, target); 
} 

的用途是:

std::shared_ptr<SomeType> bar = std::make_shared<SomeType>(); 
foo(&SomeType::function, bar); 
+1

與簽名'無效()函數'不帶參數。錯字? – bobah

+0

對,修好了! – Teris

回答

2

喜歡的東西

template <class T> 
void foo(std::function<void(KnownType)> function, T&& target) 
{ 
    std::bind(function, std::forward<T>(target)); 
} 
+0

thx,但有沒有辦法在調用foo時不指定類型T?(類似於std :: bind不需要知道具體類型的方式?) – Teris

+0

編譯器應該推斷出它,所以您應該可以調用它as'foo(function,arg)' – bobah

+0

它的確如此!謝謝! – Teris