使用std::cref
時遇到此問題。一個最小的例子是這樣的:實例化重載函數模板
template<typename Fn, typename T>
auto apply(Fn f, const T &t) -> decltype(f(t))
{
return f(t);
}
int n = 123;
apply(std::cref<int>, n); // <- compile error: can't infer type `Fn`
apply([](const int &x) { return std::cref(x); }, n); // ok
我認爲與第一例子的問題是,std::cref<T>
有兩個重載版本,一個接受const T &
和其他接受std::reference_wrapper<const T>
。是否有可能在我的情況下實例化一個特定的版本?
'申請(的static_cast <性病::的reference_wrapper(*)(const int的&)>(標準:: CREF ),N);',但什麼是錯的拉姆達? –
@PiotrSkotnicki我試圖做一些像'fmap(std :: cref,foos)'將'std :: vector'變換成'std :: vector >',而沒有lambda的語法看起來更乾淨。但這可能不可能! –
你可以添加你自己的引用包裝函數並有'fmap(mycref,foos)' –