簡體   English   中英

將std函數對象轉換回functor結構

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM