模板功能以包装C++
我正在尝试使用libgit2,并认为将通话包裹起来会给我带来例外的东西是很不错的。我的理念是,我将能够调用具有共同回报的功能。我认为我可以使用模板功能来完成此操作,但是我遇到了一些问题:
template<typename F, typename... Args>
inline void invoke(Args&&... args) {
git_error_code errCode = F(std::forward<Args>(args)...);
if (errCode != GIT_OK) {
throw GitErrorException(errCode);
}
}
这产生了一些编译器警告,该警告我不使用有效的类型,因此我认为我也可以使用一个别名:
template<typename ... Args>
using F = int (*)(Args&&... args);
template<F f, typename... Args>
inline void invoke(Args&&... args) {
git_error_code errCode = f(std::forward<Args>(args)...);
if (errCode != GIT_OK) {
throw GitErrorException(errCode);
}
}
但是这也确实如此使用代码时不起作用:
Git::invoke<git_branch_name>(&name, _ref);
我真的应该在这里依靠宏吗?
I am trying to use libgit2, and thought it would be nice to wrap the calls with something that will throw an exception for me instead. My philosophy was that I would then be able to invoke functions that have a common return typ. I figured I would be able to do it with a templated function but I am running into some issues:
template<typename F, typename... Args>
inline void invoke(Args&&... args) {
git_error_code errCode = F(std::forward<Args>(args)...);
if (errCode != GIT_OK) {
throw GitErrorException(errCode);
}
}
This generated some compiler warnings that I was not using a valid type so I figured that I could also use an alias instead:
template<typename ... Args>
using F = int (*)(Args&&... args);
template<F f, typename... Args>
inline void invoke(Args&&... args) {
git_error_code errCode = f(std::forward<Args>(args)...);
if (errCode != GIT_OK) {
throw GitErrorException(errCode);
}
}
But that also did not work when using the code as follows:
Git::invoke<git_branch_name>(&name, _ref);
Should I really have to rely on a macro instead here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要具有以下语法
(C ++ 17)
对于C ++ 14,您可以使用
使用情况。
To have following syntax
You need (C++17)
For c++14, you might do instead
with usage