Templates 使用依赖于非类型模板参数的泛型函数的签名作为模板参数
下面编译并运行的小程序允许将Templates 使用依赖于非类型模板参数的泛型函数的签名作为模板参数,templates,c++14,template-templates,Templates,C++14,Template Templates,下面编译并运行的小程序允许将无符号类型的运行时变量索引与一组具有一个无符号类型的模板参数J的模板函数桥接起来 如果需要进一步澄清,请参阅 我编写的辅助函数使用模板参数,从原始函数推断尽可能多的信息。问题是,除了创建两个包装器wrap\u foo和wrap\u zoo之外,我找不到更好的方法来定义模板参数FunWrap,而我希望直接用作模板参数foo和zoo。有办法吗 #include <iostream> #include <utility> #include <a
无符号
类型的运行时变量索引
与一组具有一个无符号
类型的模板参数J
的模板函数桥接起来
如果需要进一步澄清,请参阅
我编写的辅助函数使用模板参数,从原始函数推断尽可能多的信息。问题是,除了创建两个包装器wrap\u foo
和wrap\u zoo
之外,我找不到更好的方法来定义模板参数FunWrap
,而我希望直接用作模板参数foo
和zoo
。有办法吗
#include <iostream>
#include <utility>
#include <array>
using namespace std;
// boiler plate stuff
template <template <unsigned J> typename FunWrap, unsigned... Is>
decltype(auto) get_fun_ptr_aux(std::integer_sequence<unsigned, Is...>, unsigned i)
{
typedef decltype(&FunWrap<1>::run) FunPtr;
constexpr static std::array<FunPtr, sizeof...(Is)> fun_ptrs = { &FunWrap<Is>::run... };
return fun_ptrs[i];
}
template <template <unsigned J> typename FunWrap, unsigned N>
decltype(auto) get_fun_ptr(unsigned i)
{
return get_fun_ptr_aux<FunWrap>(std::make_integer_sequence<unsigned, N>{}, i);
}
// template functions to be bridged with runtime arguments
// two functions with same template arguments but different signature
template <unsigned J>
void foo() { cout << J << "\n"; }
template <unsigned J>
double zoo(double x) { return x + J; }
// 1 wrapper per each function
template <unsigned J>
struct wrap_foo {
static void *run() { foo<J>(); } // same signature as foo
};
template <unsigned J>
struct wrap_zoo {
static double run(double x) { return zoo<J>(x); } // same signature as zoo
};
int main()
{
unsigned index = 5;
(*get_fun_ptr<wrap_foo,10>(index))();
cout << (*get_fun_ptr<wrap_zoo,10>(index))(3.5) << "\n";
return 0;
}
#包括
#包括
#包括
使用名称空间std;
//锅炉板料
模板
decltype(自动)get_fun_ptr_aux(std::integer_序列,无符号i)
{
typedef decltype(&FunWrap::run)FunPtr;
constexpr static std::array fun_ptrs={&FunWrap::run…};
返回乐趣[i];
}
模板
decltype(自动)获取乐趣ptr(未签名i)
{
返回get_fun_ptr_aux(std::make_integer_sequence{},i);
}
//要使用运行时参数桥接的模板函数
//具有相同模板参数但签名不同的两个函数
模板
void foo(){cout
我想直接使用foo
和zoo
作为模板参数。有没有办法
#include <iostream>
#include <utility>
#include <array>
using namespace std;
// boiler plate stuff
template <template <unsigned J> typename FunWrap, unsigned... Is>
decltype(auto) get_fun_ptr_aux(std::integer_sequence<unsigned, Is...>, unsigned i)
{
typedef decltype(&FunWrap<1>::run) FunPtr;
constexpr static std::array<FunPtr, sizeof...(Is)> fun_ptrs = { &FunWrap<Is>::run... };
return fun_ptrs[i];
}
template <template <unsigned J> typename FunWrap, unsigned N>
decltype(auto) get_fun_ptr(unsigned i)
{
return get_fun_ptr_aux<FunWrap>(std::make_integer_sequence<unsigned, N>{}, i);
}
// template functions to be bridged with runtime arguments
// two functions with same template arguments but different signature
template <unsigned J>
void foo() { cout << J << "\n"; }
template <unsigned J>
double zoo(double x) { return x + J; }
// 1 wrapper per each function
template <unsigned J>
struct wrap_foo {
static void *run() { foo<J>(); } // same signature as foo
};
template <unsigned J>
struct wrap_zoo {
static double run(double x) { return zoo<J>(x); } // same signature as zoo
};
int main()
{
unsigned index = 5;
(*get_fun_ptr<wrap_foo,10>(index))();
cout << (*get_fun_ptr<wrap_zoo,10>(index))(3.5) << "\n";
return 0;
}
<> >代码> fo和<代码>动物园<代码>是模板函数,恐怕答案是否定的。C++在处理过载集/模板方面是出了名的。