如何将 noexcept 说明符添加到已定义的函数类型?
How to add noexcept specifier to already defined function type?
例如我输入:
typedef DWORD WINAPI HANDLER_FUNCTION_EX (DWORD);
我想要:
static as_noexcept<HANDLER_FUNCTION_EX>::type my_func; // forward declaration
static_assert(noexcept(my_func(0)));
我得到了类似的东西:
template<typename>
struct noexcept_trait;
// specialization to infer signature
template<typename Result, typename... Args>
struct noexcept_trait<Result(Args...)>
{
using as_noexcept = Result(Args...) noexcept;
using as_throwing = Result(Args...);
};
// since C++17 noexcept-specification is a part of the function type
// so first specialization won't match
template<typename Result, typename... Args>
struct noexcept_trait<Result(Args...) noexcept>
{
using as_noexcept = Result(Args...) noexcept;
using as_throwing = Result(Args...);
};
template<typename T>
using add_noexcept_t = typename noexcept_trait<T>::as_noexcept;
template<typename T>
using remove_noexcept_t = typename noexcept_trait<T>::as_throwing;
但是这段代码创建了全新的类型并删除了所有附加信息(调用约定、属性,例如 [[deprecated]]
)。所以这不安全。我该如何解决?
添加
template<typename Result, typename... Args>
struct noexcept_trait<Result __stdcall(Args...)>
{
using as_noexcept = Result __stdcall(Args...) noexcept;
using as_throwing = Result __stdcall(Args...);
};
和其他地方类似。但是你应该只在 !is_same_v< void(*)(), void(__stdcall *)() >
的情况下这样做。如果 __stdcall
在你的平台上什么都不做,你会在这里遇到冲突。
属性显然不是类型系统的一部分。 [[deprecated]]
适用于类型定义本身,而不是被定义的东西。使用该类型来创建别名是 [[deprecated]]
.
例如我输入:
typedef DWORD WINAPI HANDLER_FUNCTION_EX (DWORD);
我想要:
static as_noexcept<HANDLER_FUNCTION_EX>::type my_func; // forward declaration
static_assert(noexcept(my_func(0)));
我得到了类似的东西:
template<typename>
struct noexcept_trait;
// specialization to infer signature
template<typename Result, typename... Args>
struct noexcept_trait<Result(Args...)>
{
using as_noexcept = Result(Args...) noexcept;
using as_throwing = Result(Args...);
};
// since C++17 noexcept-specification is a part of the function type
// so first specialization won't match
template<typename Result, typename... Args>
struct noexcept_trait<Result(Args...) noexcept>
{
using as_noexcept = Result(Args...) noexcept;
using as_throwing = Result(Args...);
};
template<typename T>
using add_noexcept_t = typename noexcept_trait<T>::as_noexcept;
template<typename T>
using remove_noexcept_t = typename noexcept_trait<T>::as_throwing;
但是这段代码创建了全新的类型并删除了所有附加信息(调用约定、属性,例如 [[deprecated]]
)。所以这不安全。我该如何解决?
添加
template<typename Result, typename... Args>
struct noexcept_trait<Result __stdcall(Args...)>
{
using as_noexcept = Result __stdcall(Args...) noexcept;
using as_throwing = Result __stdcall(Args...);
};
和其他地方类似。但是你应该只在 !is_same_v< void(*)(), void(__stdcall *)() >
的情况下这样做。如果 __stdcall
在你的平台上什么都不做,你会在这里遇到冲突。
属性显然不是类型系统的一部分。 [[deprecated]]
适用于类型定义本身,而不是被定义的东西。使用该类型来创建别名是 [[deprecated]]
.