Transforming std::tuple<T...> to T
Transforming std::tuple<T...> to T
所以我有一个std::tuple<T...>
,我想创建一个接受T...
的函数指针,目前这是我得到的;
template<typename... Arguments>
using FunctionPointer = void (*)(Arguments...);
using FunctionPtr = FunctionPointer<typename std::tuple_element<0, V>::type,
typename std::tuple_element<1, V>::type,
typename std::tuple_element<2, V>::type>;
然而,我似乎找不到一种方法来做到这一点,而无需手动输入0, ..., tuple_size<V>::value
的每个索引。FunctionPtr是在上下文中定义的,其中V=std::tuple<T...>
(也已经有一个可变模板(因此我不能直接传递T...
))
我想我需要生成一些索引列表,并做一些黑魔法…
这是一个可能的解决方案:
#include <tuple>
// This is what you already have...
template<typename... Arguments>
using FunctionPointer = void (*)(Arguments...);
// Some new machinery the end user does not need to no about
namespace detail
{
template<typename>
struct from_tuple { };
template<typename... Ts>
struct from_tuple<std::tuple<Ts...>>
{
using FunctionPtr = FunctionPointer<Ts...>;
};
}
//=================================================================
// This is how your original alias template ends up being rewritten
//=================================================================
template<typename T>
using FunctionPtr = typename detail::from_tuple<T>::FunctionPtr;
你可以这样使用它:
// Some function to test if the alias template works correctly
void foo(int, double, bool) { }
int main()
{
// Given a tuple type...
using my_tuple = std::tuple<int, double, bool>;
// Retrieve the associated function pointer type...
using my_fxn_ptr = FunctionPtr<my_tuple>; // <== This should be what you want
// And verify the function pointer type is correct!
my_fxn_ptr ptr = &foo;
}
一个简单的trait就可以做到:
#include <tuple>
template <typename> struct tuple_to_function;
template <typename ...Args>
struct tuple_to_function<std::tuple<Args...>>
{
typedef void (*type)(Args...);
};
用法:
typedef std::tuple<Foo, Bar, int> myTuple;
tuple_to_function<myTuple>::type fp; // is a void (*)(Foo, Bar, int)
相关文章:
- "error: no matching function for call to"构造函数错误
- 调用专用模板时出错"no matching function for call to [...]"
- C++ Singleton - Prevent ::instance() to variable
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- Visual Studio Code "undefined reference to `WinMain@16'"
- 使用 GCC 卸载的 OpenMP 卸载失败,并出现"Ptx assembly aborted due to errors"
- 如何解决"invalid conversion from 'char' to 'const char*'"
- 使用 MATLAB 编码器生成C++代码:编译错误"undefined reference to `rgb2gray_tbb_real64'"
- EASTL矢量<向量<int>>连续的
- 尝试链接我的着色器时,我收到错误代码"error c5145 must write to gl_position"
- Python str to C++ to Python str
- 为什么创建友元类的实例会导致"undefined reference to"错误?
- System.InvalidCastException - SQL to C++ - safe_cast<float>
- 使用 cmake 的 Linux 终端上的"Conversion to non-scalar type is requested"错误
- "no matching function for call to 'Vector::Vector'"错误
- vector<vector<double>> to mxArray using memcpy
- Tensorflow c++ api undefined reference to 'tflite::D efaultErrorReporter()'
- 当覆盖存在时调用基本虚拟"binded to object"函数
- OpenGL VBO Indexing ( How to compute Index Array)
- 为什么我会" void value not ignored as it ought to be"?