gpt4 book ai didi

c++ - 用异常抽象包装 C 错误处理的最佳方法

转载 作者:可可西里 更新时间:2023-11-01 15:23:50 26 4
gpt4 key购买 nike

有用的 C 库不提供 C++ 绑定(bind)的情况并不少见。从 C++ 调用 C 很容易,但除其他问题外,C++ 项目可能需要异常,而不是数字错误返回值。

在每个函数调用中不包括 ifthrow 的情况下转换为异常是否有任何特定的约定或技巧?

我编写了这个用于包装 gphoto2 调用的解决方案。必须将模板函数包装在宏中很尴尬(但函数名称对于错误消息很重要;这里的逻辑类似于 perror)。

是否有更好的技术或任何开源项目可以做得特别好?

#include <gphoto2/gphoto2.h>
#include <string>

class Gphoto2Error : public std::exception {
private:
std::string message;

public:
Gphoto2Error(std::string func, int err) {
message = func + ": " + std::string(gp_result_as_string(err));
}
const char *what() const throw() {
return message.c_str();
}
};

template <typename F, typename... Args>
void _gpCall(const char *name, F f, Args... args) {
int ret = f(args...);
if (ret != GP_OK) {
throw Gphoto2Error(name, ret);
}
}
#define gpCall(f, ...) ((_gpCall(#f, ((f)), __VA_ARGS__)))

int main() {
GPContext *ctx = gp_context_new();
Camera *camera;
gpCall(gp_camera_new, &camera);
gpCall(gp_camera_init, camera, ctx);
}

最佳答案

因为有可能(在实践中,通过 __PRETTY_FUNCTION__typeid )恢复模板参数引用的函数的名称,我会(在 C++17 中)写

template<auto &F,class ...AA>
void gpCall(AA &&...aa) {
if(int ret=f(aa...); ret!=GP_OK)
throw Gphoto2Error(/* … */,ret);
}
int main() {
GPContext *ctx = gp_context_new();
Camera *camera;
gpCall<gp_camera_new>(&camera);
gpCall<gp_camera_init>(camera, ctx);
return 0;
}

获取函数名称的表达式是依赖于实现的,但在许多情况下产生可用结果(尽管有额外文本)的基本方法是添加

#include<typeinfo>
template<auto&> Symbol;

然后写typeid(Symbol<F>).name() .

关于c++ - 用异常抽象包装 C 错误处理的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56995824/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com