如何检测模板参数是否为noexcept函数? [英] How can I detect whether a template argument is a noexcept function?

查看:315
本文介绍了如何检测模板参数是否为noexcept函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  template< p>我有一个函数来生成一个lambda,作为一个函数的包装函数,我可以稍后调用:类型名F,类型名... FArgs> 
auto make_lambda(F&& f,FArgs& ... f_args)
{
return [&]() - > std :: result_of_t< F(FArgs ...)>
{
return std :: forward< F>(f)(std :: forward< FArgs>(f_args)...);
};
}



我想让返回的lambda noexcept ,因此我的函数的返回值如下所示: f p>

  return [&]()noexcept(is_noexcept< decltype(f)> :: value)
-> std :: result_of_t< F(FArgs ...)>
{
return std :: forward< F>(f)(std :: forward< FArgs>(f_args)...);
};

我的尝试:

  #include< type_traits> 

void f(){}
void g()noexcept {}

template< typename F,typename ... Args>
struct is_noexcept:std :: false_type {};

template< typename F,typename ... Args>
struct is_noexcept< F(Args ...)noexcept> :std :: true_type {};

int main()
{
bool constexpr func_test_a {is_noexcept< decltype(f)> :: value}; // true
bool constexpr func_test_b {is_noexcept< decltype(g)> :: value}; // true
}

但是,测试总是返回 true 。我缺少什么?任何人都可以提供此问题的解决方案?

解决方案

From:http://en.cppreference.com/w/cpp/language/noexcept_spec


noexcept规范不是函数类型的一部分。 (直到
C ++ 17)。


目前,模板扣除不会产生正确的结果,因为 noexcept 说明符不是函数类型的一部分;模板类型的扣除将无法工作,直到C ++ 17。我检测函数是否 noexcept 的方式将在C ++ 17中有效,因为这个回答的方式。


I have function to generate a lambda that acts as a wrapper to a function I can invoke later:

template <typename F, typename... FArgs>
auto make_lambda( F&& f, FArgs&&... f_args )
{
    return [&] () -> std::result_of_t<F( FArgs... )>
    {
        return std::forward<F>( f )( std::forward<FArgs>( f_args )... );
    };
}

I'd like to make the returned lambda noexcept when argument f is noexcept, so my function's return would look like this:

return [&] () noexcept( is_noexcept<decltype( f )>::value )
    -> std::result_of_t<F( FArgs... )>
{
    return std::forward<F>( f )( std::forward<FArgs>( f_args )... );
};

My attempt:

#include <type_traits>

void f() {}
void g() noexcept {}

template <typename F, typename... Args>
struct is_noexcept : std::false_type {};

template <typename F, typename... Args>
struct is_noexcept<F( Args... ) noexcept> : std::true_type {};

int main()
{
    bool constexpr func_test_a{ is_noexcept<decltype( f )>::value }; // true
    bool constexpr func_test_b{ is_noexcept<decltype( g )>::value }; // true
}

However, the test always returns true. What am I missing? Can anyone provide a solution to this problem?

解决方案

From: http://en.cppreference.com/w/cpp/language/noexcept_spec

The noexcept-specification is not a part of the function type. (until C++17).

Currently, template deduction will not produce the correct results since the noexcept specifier is not part of a function's type; template type deduction will not work until C++17. My way of detecting whether a function is noexcept will be valid in C++17 as will this answer's way.

这篇关于如何检测模板参数是否为noexcept函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆