簡體   English   中英

如何測試一個std :: function <T> 對於模板參數T是可構造的

[英]How to test if an std::function<T> is constructible for template argument T

我目前正在編寫一個模板類,它接受一個Signature模板參數並在內部存儲一個std :: function。

template <class Signature>
class Impl
{
    std::function<Signature> f;
};

這似乎工作得很好,除非Signature模板參數無效,編譯器在std :: function中失敗並出現一些模板實例化錯誤。

現在因為Impl客戶端不需要知道Impl的內部結構,所以最好將一些人類可讀的消息輸出給將使用Impl表示Signature參數無效的開發人員。

使用is_invocable特征類,類似於:

static_assert(is_invocable<Signature>::value, "Template parameter Signature is invalid");

在嘗試寫這樣的特質課時,我想出了這個:

template <class Signature>
struct is_invocable : std::false_type
{};

template <class Signature>
struct is_invocable <std::function<Signature>> : std::true_type
{};

但是這似乎不起作用,因為is_invocable不想檢查Signature是否是std :: function,而是可以構造一個std::function<T>其中T是模板參數Signature

怎么可能寫一個is_invocable類?

注意:c ++ 17不可用。

正如StoryTeller在評論建議的那樣,你想要std::is_function ,因為傳遞給std::function的模板參數必須是簽名

template <class Signature>
struct Impl
{
    static_assert(std::is_function<Signature>::value, "");
    std::function<Signature> f;
};

std::function將檢查其函數對象是否可以使用Signature進行調用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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