执行“不接受” on std :: function? [英] Enforce "noexcept" on std::function?
问题描述
此代码编译并运行,抛出 int
:
#include< functional>
void r(std :: function< void()noexcept> f){f(); }
void foo(){throw 1; }
int main()
{
r(foo);但是我想让编译器拒绝这行。 r(foo);
因为 r
应该只传递一个 noexcept
函数。 noexcept
说明符似乎被忽略。有什么办法实现吗?
编辑:这个问题不同于是关于noexceptness的知识应该在传递函数指针时转发吗?因为我要求一个补救,特别是在 std :: function
的情况下。
解决方案我也碰到了这个问题。我的解决方案是使用委托对象(委托到std ::函数)。代理具有无例外规范。
这里...
template< class FuncT>
struct NoExceptDelegate;
template< class R,class ... Args>
struct NoExceptDelegate< R(Args ...)>
{
NoExceptDelegate(std :: function< R(Args ...)>&&&&callback)
:callback_(move(callback))
{
if(!callback_)
{
throw std :: invalid_argument(NoExceptDelegate需要有效的回调函数);
}
}
NoExceptDelegate(const NoExceptDelegate& other)
:callback_(other.callback_)
{
}
NoExceptDelegate& operator =(const NoExceptDelegate& other)
{
if(this!=& other)
{
callback_ = other.callback_;
}
return * this;
}
NoExceptDelegate(NoExceptDelegate& other)
:callback_(move(other.callback_))
{
}
b $ b NoExceptDelegate& operator =(NoExceptDelegate&& other)
{
callback_ = move(other.callback_);
return * this;
}
template< class ... ArgsU>
R operator()(ArgsU& ... args)noexcept
{
return callback_(std :: forward< ArgsU>(args)...);
}
private:
std :: function< R(Args ...)>回电话_;
};
这通常用作异步接口中的合同,指示提供的处理程序不应抛出异常:
struct Interface
{
virtual void doSomethingAsynchronous(
NoExceptDelegate< void > onCompletionResult)= 0;
//...etc
};
由于客户端是回调提供者,NoExceptDelegate是提供者的承诺,不会失败。提供者应该确保提供的至少std :: function是可调用的。
This code compiles and runs, throwing the int
:
#include <functional>
void r( std::function<void() noexcept> f ) { f(); }
void foo() { throw 1; }
int main()
{
r(foo);
}
However I would like the compiler to reject the line r(foo);
because r
should only be passed a noexcept
function. The noexcept
specifier appears to be ignored. Is there any way to achieve that?
Edit: This question is different to Is knowledge about noexcept-ness supposed to be forwarded when passing around a function pointer? because I am asking for a remedy, specifically in the case of std::function
.
解决方案 I've also stumbled across this problem. My solution was to use a delegating object (delegating to the std::function). The delegate has a no-except specification. It could still be improved (move added, etc).
Here goes...
template <class FuncT>
struct NoExceptDelegate;
template <class R, class ... Args >
struct NoExceptDelegate<R(Args...)>
{
NoExceptDelegate(std::function<R(Args...)>&& callback)
: callback_(move(callback))
{
if (!callback_)
{
throw std::invalid_argument( "NoExceptDelegate requires a valid callback");
}
}
NoExceptDelegate(const NoExceptDelegate& other)
: callback_(other.callback_)
{
}
NoExceptDelegate& operator=(const NoExceptDelegate& other)
{
if (this != &other)
{
callback_ = other.callback_;
}
return *this;
}
NoExceptDelegate(NoExceptDelegate&& other)
: callback_(move(other.callback_))
{
}
NoExceptDelegate& operator=(NoExceptDelegate&& other)
{
callback_ = move(other.callback_);
return *this;
}
template <class...ArgsU>
R operator()(ArgsU&&... args) noexcept
{
return callback_(std::forward<ArgsU>(args)...);
}
private:
std::function<R(Args...)> callback_;
};
This is typically used as a contract in an asynchronous interface to indicate that the provided handler shall not throw e.g:
struct Interface
{
virtual void doSomethingAsynchronous(
NoExceptDelegate<void(int)> onCompletionResult) = 0;
//...etc
};
As the client is the callback provider, NoExceptDelegate is a promise from the provider that provided shall not fail. The provider should ensure that at least std::function provided is callable.
这篇关于执行“不接受” on std :: function?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!