[英]Cast std function object back to functor struct
在這種情況下:
struct Holder {
std::function<void()> f;
};
struct Functor { void operator()(){ /**/ } };
int main() {
Holder = { Functor{} };
//...
有沒有辦法稍后將f
Functor
轉換為Functor
類型?
target
成員函數是std::function
的type-unerasing cast。 您需要知道目標類型:
#include <cassert>
#include <functional>
struct Functor { void operator()(){ /**/ } };
int main()
{
std::function<void()> f = Functor();
Functor * p = f.target<Functor>();
assert(p != nullptr);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.