auto在(可能)用C++ 17引入的模板参数中有哪些优点?
它只是auto我想要实例化模板代码的自然扩展吗?
auto v1 = constant<5>; // v1 == 5, decltype(v1) is int
auto v2 = constant<true>; // v2 == true, decltype(v2) is bool
auto v3 = constant<'a'>; // v3 == 'a', decltype(v3) is char
Run Code Online (Sandbox Code Playgroud)
我还从这个语言功能中获得了什么?
我正在创建一个轻量级的跨平台插件框架,它在应用程序和插件之间使用C接口(通常但不总是用C++编写).
我帮助C++应用程序和插件编写器的挑战之一是找到一种在C接口上公开C++对象功能的简单方法.我现在的解决方案感觉很简单,并使用模板"构建"C-signature函数,根据这个伟大的stackoverflow问题和答案来包装底层的C++成员函数
template <typename Tc, typename F, F>
struct MemberFuncWrapper;
template <typename Tc, // C interface structure tag
typename T, // C++ class, derived from Tc
typename R, // C++ member function return type
typename ...Args, // C++ member function argument types
R (T::*f)(Args...) const> // C++ member function
struct MemberFuncWrapper<Tc, R (T::*)(Args...) const, f> {
static R call(const Tc * tc, Args... args) {
const T * t = static_cast<const T *>(tc);
return ((*t).*f)(args...);
}
};
Run Code Online (Sandbox Code Playgroud)
该模板的实例化在linux(gcc)和mac(clang)下编译并运行良好,但Visual …
如何通过函数传递成员函数指针std::function.我将通过比较(实时测试)来解释它:
template<class R, class... FArgs, class... Args>
void easy_bind(std::function<R(FArgs...)> f, Args&&... args){
}
int main() {
fx::easy_bind(&Class::test_function, new Class);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我收到一条错误消息:
no matching function for call to ‘easy_bind(void (Class::*)(int, float, std::string), Class*)’
Run Code Online (Sandbox Code Playgroud)
我只是不明白为什么函数指针std::function在通过函数参数传递时无法传递给它.我该如何通过该功能?我愿意将easy_bind函数参数更改std::function为函数指针,但我真的不知道如何.
编辑:问题简化.
编辑:感谢@remyabel,我得到了我需要的东西:http://ideone.com/FtkVBg
template <typename R, typename T, typename... FArgs, typename... Args>
auto easy_bind(R (T::*mf)(FArgs...), Args&&... args)
-> decltype(fx::easy_bind(std::function<R(T*,FArgs...)>(mf), args...)) {
return fx::easy_bind(std::function<R(T*,FArgs...)>(mf), args...);
}
Run Code Online (Sandbox Code Playgroud)