如何解决如何在 C++20 中的模板化函子中处理 void 返回类型?
我已经构建了一个模板化的函子对象,我可以用它来管理需要递归和跨范围生存的 lambda。它不是很漂亮(它使用空指针和 std::function
实例),但它适用于我需要的用例。 (如果发帖人可以不评论它的类型安全和非常糟糕的做法,我将不胜感激。我知道。)
但是,它有一个明显的问题:它无法处理返回 void
的 lambda,因为某些路径尝试将返回值存储在变量中。我需要知道如何使用 if constexpr
语句来检测函子的 lambda 的结果是否为空,并适当地处理它。这不是一个独特的问题,但我发现的所有结果都非常过时,其中许多使用现已折旧的 result_of_t
。
任何帮助将不胜感激。
#include <iostream>
#include <string>
#include <functional>
#define uint unsigned int
//! A standardised wrapper for lambda functions,which can be stored in pointers,used recursively,keep track of external storage via a void *,and set to self destruct when no longer useful.
template <class F,bool UsesDataStorage>
class Functor {
protected:
std::function<F> m_f; //!< The lambda stored by the wrapper
void* m_data = nullptr; //!< A void pointer which will be given to the lambda if `UsesDataStorage`. Note that cleanup is delegated to the lambda; the functor instance will not handle it.
bool m_selfDestructing = true; //!< Whether the combinator will self-destruct should its lambda mark itself as no longer useful.
bool m_selfDestructTrigger = false; //!< Whether the combinator's lambda has marked itself as no longer useful.
public:
inline bool usesDataStorage() const { return UsesDataStorage; } //!< Return whether this functor is set up to give its function a `data` void-pointer,which will presumably be set to a data-structure.
inline void* getData() const { return m_data; } //!< Returns the void pointer which is passed to the lambda at each call (if the functor instance uses data storage).
inline void setData(void* data) { m_data = data; } //!< Sets the void pointer which is passed to the lambda at each call (if the functor instance uses data storage).
inline bool canSelfDestruct() const { return m_selfDestructing; } //!< Returns whether the LambdaWrapper will delete itself when instructed to by the contained lambda.
inline void triggerSelfDestruct() { m_selfDestructTrigger = true; } //!< Triggers wrapper self-deletion at the end of ruinning the lambda.
Functor(const std::function<F>& f,bool canSelfDestruct = true) :
m_f(f),m_selfDestructing(canSelfDestruct)
{} //!< Constructor for Functor instances which DON'T use data storage. Note that the given function should always take a void pointer as the first argument,which is where a pointer to the Functor instance will be passed.
Functor(std::function<F>&& f,which is where a pointer to the Functor instance will be passed.
Functor(const std::function<F>& f,void* data,m_data(data),m_selfDestructing(canSelfDestruct)
{} //!< Constructor for Functor instances which DO use data storage. Note that the given function should always take a void pointer as the first argument,which is where a pointer to the Functor instance will be passed,and a void * for the second argument,which is where the data storage pointer is passed.
Functor(std::function<F>&& f,which is where the data storage pointer is passed.
template <typename... Args>
decltype(auto) operator()(Args&&... args) {
// Avoid storing return if we can,if (!m_selfDestructing) {
if constexpr (UsesDataStorage) {
// Pass itself to m_f,then the data storage,then the arguments.
// This should work even if the return type is void,as far as I can tell.
return m_f(this,m_data,std::forward<Args>(args)...);
}
else {
// Pass itself to m_f,std::forward<Args>(args)...);
}
}
else {
if constexpr (UsesDataStorage) {
// Pass itself to m_f,then the arguments.
// ----- !!! -----
// The following if constexpr statement is what I can't work out how to do.
// ----- !!! -----
if constexpr (std::is_same<std::invoke_result_t<std::function<F>>,void>) {
m_f(this,std::forward<Args>(args)...);
// self-destruct if necessary,allowing lamdas to delete themselves if they know they're no longer useful.
if (m_selfDestructTrigger) { delete this; }
return;
}
else {
auto r = m_f(this,allowing lamdas to delete themselves if they know they're no longer useful.
if (m_selfDestructTrigger) { delete this; }
return r;
}
}
else {
// Pass itself to m_f,allowing lamdas to delete themselves if they know they're no longer useful.
if (m_selfDestructTrigger) { delete this; }
return r;
}
}
}
}
};
template <class F> Functor(std::function<F>,bool)->Functor<F,false>;
template <class F> Functor(std::function<F>,void*,true>;
int main() {
Functor f1 = Functor(std::function([](void* self,uint val1) -> uint {
std::cout << "f1(" << val1 << ") was called." << std::endl;
return 2u * val1;
}),false);
Functor f2 = Functor(std::function([](void* self,uint val1) -> void {
std::cout << "f2(" << val1 << ") was called." << std::endl;
return;
}),false);
auto x = f1(3u); // Compiles and works.
f2(3u); // Doesn't compile.
}
解决方法
我正在寻找的行是这样的:
if constexpr (std::is_same<std::function<F>::result_type,void>::value) {}
非常感谢@NathanOliver!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。