有没有办法在运行时根据整数调用函数? [英] is there a way to call a function depending on an integer at runtime?
问题描述
我有这样的场景:几个数组,我在编译时有固定的
值。现在,在运行时我需要访问一个
特定的数组,具体取决于整数但是我想避免if和
切换语句。
我的第一种方法是依靠部分模板专业化。
因此,我有:
// .h文件
template< int vSomeClass;
template< struct SomeClass< 1 {
static double loc(unsigned short i){
返回loc_ [i];
}
static const double loc_ [1];
};
template< struct SomeClass< 2 {
static double loc(unsigned short i){
return loc_ [i];
}
static const double loc_ [2];
};
//等等,直到30个部分专业化
>
// .cpp文件
const double SomeClass< 1> :: loc_ [] = {0.4};
const double SomeClass< 2> :: loc_ [] = {0.2,0.6};
//等等,直到30个部分专业化
所以我调用了这样的函数:
double location = SomeClass< 3> :: loc(2);
嗯,这种方法工作正常,直到我更改代码,以便
整数直到运行时才知道。所以问题是......
有没有办法在没有开关或
语句的情况下在运行时做同样的事情?有没有办法在运行时执行函数重载?我认为我可以拥有以下内容:
struct SomeClass {
int val;
SomeClass(int v):val(v){}
double loc(unsigned short i){
返回locImpl( classB(val),i);
}
double locImpl(classB(1),int i){
const double SomeClass< 1> ; :: loc_ [] = {0.4};
返回loc_ [i];
}
double locImpl(classB(2), int i){
const double SomeClass< 2> :: loc_ [] = {0.2,0.6};
返回loc_ [i];
}
};
并将整数转换为某个类并依赖函数
重载,但我无法'找不到办法做到这一点。有任何想法吗?感谢
你,
a2
I have this scenario: several arrays for which I have their fixed
values at compilation time. Now, at runtime I need to access a
specific array depending on an integer but I want to avoid if and
switch statements.
My first approach was to rely on partial template specialization.
Therefore, I have:
// .h file
template <int vSomeClass;
template <struct SomeClass<1{
static double loc(unsigned short i) {
return loc_[i];
}
static const double loc_[1];
};
template <struct SomeClass<2{
static double loc(unsigned short i) {
return loc_[i];
}
static const double loc_[2];
};
// and so on until 30 partial specializations
// .cpp file
const double SomeClass<1>::loc_[] = { 0.4 };
const double SomeClass<2>::loc_[] = { 0.2, 0.6 };
// and so on until 30 partial specializations
so I called the function like this:
double location = SomeClass<3>::loc(2);
Well, this approach worked fine until I changed the code so that the
integer is not known until runtime. So the question is...
Is there a way to do the same at runtime without having a switch or if
statement? Is there a way to do function overloading at runtime? I
thought that I could have something like the following:
struct SomeClass {
int val;
SomeClass(int v) : val(v) {}
double loc(unsigned short i) {
return locImpl(classB(val),i);
}
double locImpl(classB(1), int i) {
const double SomeClass<1>::loc_[] = { 0.4};
return loc_[i];
}
double locImpl(classB(2), int i) {
const double SomeClass<2>::loc_[] = { 0.2, 0.6 };
return loc_[i];
}
};
and convert the integer to some class and rely on function
overloading, but I couldn''t find a way to do this. Any ideas? Thank
you,
a2
推荐答案
aaragon< al * *************@gmail.com写道:
aaragon <al**************@gmail.comwrote:
我有这样的场景:我已修复了几个数组
编译时的
值。现在,在运行时我需要访问一个
特定的数组,具体取决于整数,但我想避免if和
切换语句。
I have this scenario: several arrays for which I have their fixed
values at compilation time. Now, at runtime I need to access a
specific array depending on an integer but I want to avoid if and
switch statements.
地图< int,vector<我认为,双重服务会非常好。
A map< int, vector< double would serve quite nicely I think.
On 12 ??? 29 ??¥,?????? 11 ??? 08 ???,aaragon< alejandro.ara ... @ gmail.comwrote:
On 12???29??¥, ??????11???08???, aaragon<alejandro.ara...@gmail.comwrote:
我有这样的场景:我有几个数组,我有固定的
值编译时间。现在,在运行时我需要访问一个
特定的数组,具体取决于整数但是我想避免if和
切换语句。
我的第一种方法是依靠部分模板专业化。
因此,我有:
// .h文件
template< int vSomeClass;
template< struct SomeClass< 1 {
?* static double loc(unsigned short i){
?*?* return loc_ [i];
?*}
?* static const double loc_ [1];};
模板< struct SomeClass< 2 {
?* static double loc(unsigned short i){
?*?* return loc_ [我];
?*}
?* static const double loc_ [2];};
//等等直到30个部分专业化
// .cpp文件
const double SomeClass< 1> :: loc_ [] = {0.4};
const double SomeClass< 2> :: loc_ [] = {0.2,0.6};
//等等,直到30个部分特化
所以我调用了这样的函数:
double location = SomeClass< 3> :: loc(2);
嗯,这个方法工作正常,直到我更改代码,以便在运行时之前不知道
整数。所以问题是......
有没有办法在没有开关或
语句的情况下在运行时做同样的事情?有没有办法在运行时执行函数重载?我认为我可以拥有以下内容:
struct SomeClass {
?* int val ;
?* SomeClass(int v):val(v){}
?* double loc(unsigned short i){
?*?*返回locImpl(classB(val),i);
?*}
?* double locImpl(classB(1),int i) {
?*?* const double SomeClass< 1> :: loc_ [] = {0.4};
?*?* return loc_ [i];
?*}
?* double locImpl(classB(2),int i){
?*?* const double SomeClass< 2> :: loc_ [] = {0.2,0.6};
?*?*返回loc_ [i];
?*}
};
并将整数转换为某个类并依赖函数
重载,但我找不到办法做到这一点。有任何想法吗?感谢
你,
a?2
I have this scenario: several arrays for which I have their fixed
values at compilation time. Now, at runtime I need to access a
specific array depending on an integer but I want to avoid if and
switch statements.
My first approach was to rely on partial template specialization.
Therefore, I have:
// .h file
template <int vSomeClass;
template <struct SomeClass<1{
?* static double loc(unsigned short i) {
?* ?* return loc_[i];
?* }
?* static const double loc_[1];};
template <struct SomeClass<2{
?* static double loc(unsigned short i) {
?* ?* return loc_[i];
?* }
?* static const double loc_[2];};
// and so on until 30 partial specializations
// .cpp file
const double SomeClass<1>::loc_[] = { 0.4 };
const double SomeClass<2>::loc_[] = { 0.2, 0.6 };
// and so on until 30 partial specializations
so I called the function like this:
double location = SomeClass<3>::loc(2);
Well, this approach worked fine until I changed the code so that the
integer is not known until runtime. So the question is...
Is there a way to do the same at runtime without having a switch or if
statement? Is there a way to do function overloading at runtime? I
thought that I could have something like the following:
struct SomeClass {
?* int val;
?* SomeClass(int v) : val(v) {}
?* double loc(unsigned short i) {
?* ?* return locImpl(classB(val),i);
?* }
?* double locImpl(classB(1), int i) {
?* ?* const double SomeClass<1>::loc_[] = { 0.4};
?* ?* return loc_[i];
?* }
?* double locImpl(classB(2), int i) {
?* ?* const double SomeClass<2>::loc_[] = { 0.2, 0.6 };
?* ?* return loc_[i];
?* }
};
and convert the integer to some class and rely on function
overloading, but I couldn''t find a way to do this. Any ideas? Thank
you,
a?2
map< int,vector< double是一个很好的方法。除此之外,你可以试试
来使用boost \ preprocessor。像这样:
#define LocItem(z,n,...)SomeClass< n + 1> :: loc_,
const double * locs [ ] =
{
BOOST_PP_REPEAT(30,LocItem,)
};
double loc (int v,unsigned short i)
{
返回locs [v - 1] [i]
}
我认为v是1到30.
map< int, vector< double is a good approach. Beyond it you can try
to use boost\preprocessor. Like this:
#define LocItem(z, n, ...) SomeClass<n+1>::loc_,
const double* locs[] =
{
BOOST_PP_REPEAT(30, LocItem,)
};
double loc(int v, unsigned short i)
{
return locs[v - 1][i]
}
I suppose v is from 1 to 30.
12月28日,晚上10点40分,KL < windleaf_2 ... @ 163.comwrote:
On Dec 28, 10:40 pm, "K.L." <windleaf_2...@163.comwrote:
12 12 ??? 29 ??¥,?????? 11 ??? 08 ?? ?,aaragon< alejandro.ara ... @ gmail.comwrote:
On 12???29??¥, ??????11???08???, aaragon <alejandro.ara...@gmail.comwrote:
我有这样的场景:几个阵列我已经固定了
编译时的值。现在,在运行时我需要访问一个
特定的数组,具体取决于整数,但我想避免if和
切换语句。
I have this scenario: several arrays for which I have their fixed
values at compilation time. Now, at runtime I need to access a
specific array depending on an integer but I want to avoid if and
switch statements.
我的第一种方法是依靠部分模板专业化。
因此,我有:
My first approach was to rely on partial template specialization.
Therefore, I have:
// .h文件
模板< int vSomeClass;
// .h file
template <int vSomeClass;
template< struct SomeClass< 1 {
static double loc(unsigned short i){
返回loc_ [i];
}
static const double loc_ [1];};
template <struct SomeClass<1{
static double loc(unsigned short i) {
return loc_[i];
}
static const double loc_[1];};
template< struct SomeClass< 2 {
static double loc(unsigned short i){
返回loc_ [i];
}
static const double loc_ [2];};
template <struct SomeClass<2{
static double loc(unsigned short i) {
return loc_[i];
}
static const double loc_[2];};
//等等,直到30个部分特化
// and so on until 30 partial specializations
/ / .cpp文件
const double SomeClass< 1> :: loc_ [] = {0.4};
const double SomeClass< 2> :: loc_ [] = {0.2 ,0.6};
//等等,直到30个部分特化
// .cpp file
const double SomeClass<1>::loc_[] = { 0.4 };
const double SomeClass<2>::loc_[] = { 0.2, 0.6 };
// and so on until 30 partial specializations
所以我调用了这样的函数:
so I called the function like this:
double location = SomeClass< 3> :: loc(2);
double location = SomeClass<3>::loc(2);
嗯,这种方法工作正常,直到我更改代码,以便
整数在运行时才知道。所以问题是......
有没有办法在没有开关或
语句的情况下在运行时做同样的事情?有没有办法在运行时执行函数重载?我认为我可能会得到以下内容:
Well, this approach worked fine until I changed the code so that the
integer is not known until runtime. So the question is...
Is there a way to do the same at runtime without having a switch or if
statement? Is there a way to do function overloading at runtime? I
thought that I could have something like the following:
struct SomeClass {
struct SomeClass {
int val;
SomeClass(int v):val(v){}
int val;
SomeClass(int v) : val(v) {}
double loc(unsigned short i){
返回locImpl(classB(val),i);
}
double locImpl(classB(1),int i){
const double SomeClass< 1> :: loc_ [] = {0.4};
return loc_ [i];
}
double locImpl(classB(2),int i){
const double SomeClass< 2> :: loc_ [] = {0.2 ,0.6};
返回loc_ [i];
}
double loc(unsigned short i) {
return locImpl(classB(val),i);
}
double locImpl(classB(1), int i) {
const double SomeClass<1>::loc_[] = { 0.4};
return loc_[i];
}
double locImpl(classB(2), int i) {
const double SomeClass<2>::loc_[] = { 0.2, 0.6 };
return loc_[i];
}
};
};
并将整数转换为某个类并依赖函数
重载,但我找不到方法去做这个。有任何想法吗?感谢
你,
and convert the integer to some class and rely on function
overloading, but I couldn''t find a way to do this. Any ideas? Thank
you,
a?2
a?2
map< ; int,vector< double是一个很好的方法。除此之外,你可以试试
来使用boost \ preprocessor。像这样:
#define LocItem(z,n,...)SomeClass< n + 1> :: loc_,
const double * locs [ ] =
{
BOOST_PP_REPEAT(30,LocItem,)
};
double loc(int v,unsigned short i)
{
返回locs [v - 1] [i]
}
我认为v是从1到30.
map< int, vector< double is a good approach. Beyond it you can try
to use boost\preprocessor. Like this:
#define LocItem(z, n, ...) SomeClass<n+1>::loc_,
const double* locs[] =
{
BOOST_PP_REPEAT(30, LocItem,)
};
double loc(int v, unsigned short i)
{
return locs[v - 1][i]
}
I suppose v is from 1 to 30.
使用地图的问题是它需要用
实例化其中的每个元素(需要创建所有向量)。我想
使用一个函数,因为这样只使用我使用的向量
初始化。最有可能的是,每次运行我将只使用这30个向量中的一个
,因此初始化所有这些向量都没有意义。任何
其他想法?
The problem with using a map is that it needs to be instantiated with
every element in it (all vectors needed to be created). I wanted to
use a function because in that way only the vectors that I use are
initialized. Most likely, I will be using only 1 of those 30 vectors
for each run, so it doesn''t make sense to initialize ALL of them. Any
other ideas?
这篇关于有没有办法在运行时根据整数调用函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!