繁体   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