473,320 Members | 1,957 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

is there a way to call a function depending on an integer at runtime?

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,


Dec 29 '07 #1
12 2260
aaragon <al**************@gmail.comwrote:
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.
A map< int, vector< double would serve quite nicely I think.
Dec 29 '07 #2
On 12月29æ—¥, 上åˆ11æ—¶08分, aaragon<alejandro.ara...@gmail.comwrote:
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²
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.
Dec 29 '07 #3
On Dec 28, 10:40 pm, "K.L." <windleaf_2...@163.comwrote:
On 12月29æ—¥, 上åˆ11æ—¶08分, aaragon <alejandro.ara...@gmail.comwrote:
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²

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.
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?
Dec 29 '07 #4
aaragon wrote:
On Dec 28, 10:40 pm, "K.L." <windleaf_2...@163.comwrote:
>On 12?29?, ??11?08?, aaragon <alejandro.ara...@gmail.comwrote:
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,

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.

The problem with using a map is that it needs to be instantiated with
every element in it (all vectors needed to be created).
Huh? What makes you think that?

std::map< int, std::vector<double the_map;

creates an empty map. The expression

the_map[i]

returns a reference to a vector<doubleand this vector will be created (and
default initialized) at the first time a particular value for i is used. No
vectors will be created for indices that are not used.

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.
If you only use i=5, then only that map-entry will exist and no other
vectors will be created.

[snip]
Best

Kai-Uwe Bux
Dec 29 '07 #5
aaragon <al**************@gmail.comwrote:
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.
Unless a single run is extremely fast, and you will be running this
program thousands of times per second, the extra time it take to
initialize all of the vectors is irrelevant. And if the run is extremely
fast and you are running the program thousands of times per second, then
you should be embedding this code in a program that makes several passes
per run.

So yes, it does make sense to initialize all of them.
Dec 29 '07 #6
On Dec 29, 10:52 am, "Daniel T." <danie...@earthlink.netwrote:
aaragon <alejandro.ara...@gmail.comwrote:
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.

Unless a single run is extremely fast, and you will be running this
program thousands of times per second, the extra time it take to
initialize all of the vectors is irrelevant. And if the run is extremely
fast and you are running the program thousands of times per second, then
you should be embedding this code in a program that makes several passes
per run.

So yes, it does make sense to initialize all of them.
Ok, so I decided to follow your advice but with a little twist.
Instead of having a map to a vector, I will have now a map to function
pointers. Inside those functions the arrays (known at compilation
time) are declared static so they're initialized only once (when I
call the function the first time). In this way, I don't need to
initialize what I won't use. Also, I don'tneed to have annoying switch
or if blocks. It looks like this:

// .h file
template <int v>
double loc(size_t);
// forward declarations of function specializations (defined in .cxx
file)
template <>
double loc<1>(size_t);
template <>
double loc<2>(size_t);
// and so on

template <
class MapPolicy = std::map<size_t, double (*)(size_t)>
>
struct Map {

typedef MapPolicy MapType;
MapType loc_;

Map() : loc_() {
// add location function pointers
loc_[1] = &loc<1>; // ERROR!!!
loc_[2] = &loc<2>; // ERROR!!!
// and so on
}
inline double loc(size_t i, size_t gp) {
return loc_[gp](i);
}
};

// .cxx file, partial specializations for the function
template<>
double loc<1>(size_t i) {
static const double loc[] = { 0.4 };
return loc[i];
}
template<>
double loc<2>(size_t i) {
static const double loc[] = { 0.2, 0.6 };
return loc[i];
}
// and so on until 30

Ok, this looks good and it's just what I want. However, for some
strange reason that I really don't see, I cannot compile this code. At
those ERROR lines I have the following message:

testMap.h: In constructor 'fea::Map<MapPolicy>::Map()':
testMap.h:36: error: expected primary-expression before ';' token

It doesn't allow me to take the address of the templated function. The
strange thing is that I can do the same from the main.cxx file, that
is:

// main.cxx

std::map<size_t, double (*)(size_t)location;

location[1] = &loc<1>;
location[2] = &loc<2>;

cout<<"what the -"<<location[2](1)<<endl;

works fine! Is this a problem with my compiler or there is something
wrong with the syntax? I'm using gcc version 4.1.2. I know, I know...
I could just use regular functions and call them loc1, loc2 and so on
and forget about the template (this will definitely work), but hey,
I'm learning day by day and I just cannot give up that easily, right?


Dec 29 '07 #7
On Dec 29, 2:09 pm, aaragon <alejandro.ara...@gmail.comwrote:
On Dec 29, 10:52 am, "Daniel T." <danie...@earthlink.netwrote:
aaragon <alejandro.ara...@gmail.comwrote:
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.
Unless a single run is extremely fast, and you will be running this
program thousands of times per second, the extra time it take to
initialize all of the vectors is irrelevant. And if the run is extremely
fast and you are running the program thousands of times per second, then
you should be embedding this code in a program that makes several passes
per run.
So yes, it does make sense to initialize all of them.

Ok, so I decided to follow your advice but with a little twist.
Instead of having a map to a vector, I will have now a map to function
pointers. Inside those functions the arrays (known at compilation
time) are declared static so they're initialized only once (when I
call the function the first time). In this way, I don't need to
initialize what I won't use. Also, I don'tneed to have annoying switch
or if blocks. It looks like this:

// .h file
template <int v>
double loc(size_t);
// forward declarations of function specializations (defined in .cxx
file)
template <>
double loc<1>(size_t);
template <>
double loc<2>(size_t);
// and so on

template <
class MapPolicy = std::map<size_t, double (*)(size_t)>

struct Map {

typedef MapPolicy MapType;
MapType loc_;

Map() : loc_() {
// add location function pointers
loc_[1] = &loc<1>; // ERROR!!!
loc_[2] = &loc<2>; // ERROR!!!
// and so on
}
inline double loc(size_t i, size_t gp) {
return loc_[gp](i);
}

};

// .cxx file, partial specializations for the function
template<>
double loc<1>(size_t i) {
static const double loc[] = { 0.4 };
return loc[i];}

template<>
double loc<2>(size_t i) {
static const double loc[] = { 0.2, 0.6 };
return loc[i];}

// and so on until 30

Ok, this looks good and it's just what I want. However, for some
strange reason that I really don't see, I cannot compile this code. At
those ERROR lines I have the following message:

testMap.h: In constructor 'fea::Map<MapPolicy>::Map()':
testMap.h:36: error: expected primary-expression before ';' token

It doesn't allow me to take the address of the templated function. The
strange thing is that I can do the same from the main.cxx file, that
is:

// main.cxx

std::map<size_t, double (*)(size_t)location;

location[1] = &loc<1>;
location[2] = &loc<2>;

cout<<"what the -"<<location[2](1)<<endl;

works fine! Is this a problem with my compiler or there is something
wrong with the syntax? I'm using gcc version 4.1.2. I know, I know...
I could just use regular functions and call them loc1, loc2 and so on
and forget about the template (this will definitely work), but hey,
I'm learning day by day and I just cannot give up that easily, right?

Ok, I know you guys hate when people post twice, but there is no edit
button here. Strange that it seems to me, I solved the problem by
adding the namespace at the front of the function. Why did they came
up with this rule? I don't have a clue. Anyways, I think I finally got
what I wanted, this is actually cool stuff! Thank you all for the
help...


Dec 29 '07 #8
fl
On 29 déc, 15:09, aaragon <alejandro.ara...@gmail.comwrote:
On Dec 29, 10:52 am, "Daniel T." <danie...@earthlink.netwrote:


aaragon <alejandro.ara...@gmail.comwrote:
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.
Unless a single run is extremely fast, and you will be running this
program thousands of times per second, the extra time it take to
initialize all of the vectors is irrelevant. And if the run is extremely
fast and you are running the program thousands of times per second, then
you should be embedding this code in a program that makes several passes
per run.
So yes, it does make sense to initialize all of them.

Ok, so I decided to follow your advice but with a little twist.
Instead of having a map to a vector, I will have now a map to function
pointers. Inside those functions the arrays (known at compilation
time) are declared static so they're initialized only once (when I
call the function the first time). In this way, I don't need to
initialize what I won't use. Also, I don'tneed to have annoying switch
or if blocks. It looks like this:

// .h file
template <int v>
double loc(size_t);
// forward declarations of function specializations (defined in .cxx
file)
template <>
double loc<1>(size_t);
template <>
double loc<2>(size_t);
// and so on
Hi,
It is interesting. Could you fill the specific contents of the <>, so
I can learn something from you? Thanks a lot.

template <???????>
double loc<1>(size_t); // Really it is loc<1>?
template <???????>
double loc<2>(size_t);

>
template <
class MapPolicy = std::map<size_t, double (*)(size_t)>

struct Map {

* typedef MapPolicy MapType;
* MapType loc_;

* Map() : loc_() {
* // add location function pointers
* loc_[1] = &loc<1>; * *// ERROR!!!
* loc_[2] = &loc<2>; * *// ERROR!!!
* // and so on
* }
* inline double loc(size_t i, size_t gp) {
* * return loc_[gp](i);
* }

};

// .cxx file, partial specializations for the function
template<>
double loc<1>(size_t i) {
* static const double loc[] = { 0.4 };
* return loc[i];}

template<>
double loc<2>(size_t i) {
* static const double loc[] = { 0.2, 0.6 };
* return loc[i];}

// and so on until 30

Ok, this looks good and it's just what I want. However, for some
strange reason that I really don't see, I cannot compile this code. At
those ERROR lines I have the following message:

testMap.h: In constructor 'fea::Map<MapPolicy>::Map()':
testMap.h:36: error: expected primary-expression before ';' token

It doesn't allow me to take the address of the templated function. The
strange thing is that I can do the same from the main.cxx file, that
is:

// main.cxx

* * std::map<size_t, double (*)(size_t)location;

* * location[1] = &loc<1>;
* * location[2] = &loc<2>;

* * cout<<"what the -"<<location[2](1)<<endl;

works fine! Is this a problem with my compiler or there is something
wrong with the syntax? I'm using gcc version 4.1.2. I know, I know...
I could just use regular functions and call them loc1, loc2 and so on
and forget about the template (this will definitely work), but hey,
I'm learning day by day and I just cannot give up that easily, right?

a²- Masquer le texte des messages précédents -

- Afficher le texte des messages précédents -
Dec 29 '07 #9
On Dec 29, 4:44 pm, fl <rxjw...@gmail.comwrote:
On 29 déc, 15:09, aaragon <alejandro.ara...@gmail.comwrote:
On Dec 29, 10:52 am, "Daniel T." <danie...@earthlink.netwrote:
aaragon <alejandro.ara...@gmail.comwrote:
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.
Unless a single run is extremely fast, and you will be running this
program thousands of times per second, the extra time it take to
initialize all of the vectors is irrelevant. And if the run is extremely
fast and you are running the program thousands of times per second, then
you should be embedding this code in a program that makes several passes
per run.
So yes, it does make sense to initialize all of them.
Ok, so I decided to follow your advice but with a little twist.
Instead of having a map to a vector, I will have now a map to function
pointers. Inside those functions the arrays (known at compilation
time) are declared static so they're initialized only once (when I
call the function the first time). In this way, I don't need to
initialize what I won't use. Also, I don'tneed to have annoying switch
or if blocks. It looks like this:
// .h file
template <int v>
double loc(size_t);
// forward declarations of function specializations (defined in .cxx
file)
template <>
double loc<1>(size_t);
template <>
double loc<2>(size_t);
// and so on

Hi,
It is interesting. Could you fill the specific contents of the <>, so
I can learn something from you? Thanks a lot.

template <???????>
double loc<1>(size_t); // Really it is loc<1>?
template <???????>
double loc<2>(size_t);
template <
class MapPolicy = std::map<size_t, double (*)(size_t)>
struct Map {
typedef MapPolicy MapType;
MapType loc_;
Map() : loc_() {
// add location function pointers
loc_[1] = &loc<1>; // ERROR!!!
loc_[2] = &loc<2>; // ERROR!!!
// and so on
}
inline double loc(size_t i, size_t gp) {
return loc_[gp](i);
}
};
// .cxx file, partial specializations for the function
template<>
double loc<1>(size_t i) {
static const double loc[] = { 0.4 };
return loc[i];}
template<>
double loc<2>(size_t i) {
static const double loc[] = { 0.2, 0.6 };
return loc[i];}
// and so on until 30
Ok, this looks good and it's just what I want. However, for some
strange reason that I really don't see, I cannot compile this code. At
those ERROR lines I have the following message:
testMap.h: In constructor 'fea::Map<MapPolicy>::Map()':
testMap.h:36: error: expected primary-expression before ';' token
It doesn't allow me to take the address of the templated function. The
strange thing is that I can do the same from the main.cxx file, that
is:
// main.cxx
std::map<size_t, double (*)(size_t)location;
location[1] = &loc<1>;
location[2] = &loc<2>;
cout<<"what the -"<<location[2](1)<<endl;
works fine! Is this a problem with my compiler or there is something
wrong with the syntax? I'm using gcc version 4.1.2. I know, I know...
I could just use regular functions and call them loc1, loc2 and so on
and forget about the template (this will definitely work), but hey,
I'm learning day by day and I just cannot give up that easily, right?
a²- Masquer le texte des messages précédents -
- Afficher le texte des messages précédents -
Well, this is just a function templated by an integer constant that is
known at compilation time. So you can have the same function for
different values of the integer value. So first declare the function
but don't define it:

template <int v>
double loc(size_t);

and then using template specialization you can have as many functions
as you want with the same name:

template <>
double loc<1>(size_t) { // yes, it is actually loc<1>
// do whatever you want to do here
}
// and the same for all other functions

Then you can call the functions just by using

cout<<"using loc<1>: "<<loc<1>(5)<<endl;

The cool thing is that the map maps integer to function pointers, and
these execute the right function. I wanted to do this because I have
very big arrays to be initialized but for a particular run I don't use
them all. Therefore, it didn't make sense to me to initialize all of
them. So, I declared the known arrays as static arrays inside the
functions so they are initialized only once. And the best part, is
that those functions that I don't use, don't initialize any arrays...
=)
This is just what I was looking for.

Cheers,



Dec 30 '07 #10
fl
On 29 déc, 15:09, aaragon <alejandro.ara...@gmail.comwrote:
On Dec 29, 10:52 am, "Daniel T." <danie...@earthlink.netwrote:


aaragon <alejandro.ara...@gmail.comwrote:
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.
Unless a single run is extremely fast, and you will be running this
program thousands of times per second, the extra time it take to
initialize all of the vectors is irrelevant. And if the run is extremely
fast and you are running the program thousands of times per second, then
you should be embedding this code in a program that makes several passes
per run.
So yes, it does make sense to initialize all of them.

Ok, so I decided to follow your advice but with a little twist.
Instead of having a map to a vector, I will have now a map to function
pointers. Inside those functions the arrays (known at compilation
time) are declared static so they're initialized only once (when I
call the function the first time). In this way, I don't need to
initialize what I won't use. Also, I don'tneed to have annoying switch
or if blocks. It looks like this:

// .h file
template <int v>
double loc(size_t);
// forward declarations of function specializations (defined in .cxx
file)
template <>
double loc<1>(size_t);
template <>
double loc<2>(size_t);
// and so on

template <
class MapPolicy = std::map<size_t, double (*)(size_t)>

struct Map {

* typedef MapPolicy MapType;
* MapType loc_;

* Map() : loc_() {
* // add location function pointers
* loc_[1] = &loc<1>; * *// ERROR!!!
* loc_[2] = &loc<2>; * *// ERROR!!!
* // and so on
* }
* inline double loc(size_t i, size_t gp) {
* * return loc_[gp](i);
* }

};

// .cxx file, partial specializations for the function
template<>
double loc<1>(size_t i) {
* static const double loc[] = { 0.4 };
* return loc[i];}

template<>
double loc<2>(size_t i) {
* static const double loc[] = { 0.2, 0.6 };
* return loc[i];}

// and so on until 30

Ok, this looks good and it's just what I want. However, for some
strange reason that I really don't see, I cannot compile this code. At
those ERROR lines I have the following message:

testMap.h: In constructor 'fea::Map<MapPolicy>::Map()':
testMap.h:36: error: expected primary-expression before ';' token

It doesn't allow me to take the address of the templated function. The
strange thing is that I can do the same from the main.cxx file, that
is:

// main.cxx

* * std::map<size_t, double (*)(size_t)location;

* * location[1] = &loc<1>;
* * location[2] = &loc<2>;

* * cout<<"what the -"<<location[2](1)<<endl;

works fine! Is this a problem with my compiler or there is something
wrong with the syntax? I'm using gcc version 4.1.2. I know, I know...
I could just use regular functions and call them loc1, loc2 and so on
and forget about the template (this will definitely work), but hey,
I'm learning day by day and I just cannot give up that easily, right?

a²- Masquer le texte des messages précédents -

- Afficher le texte des messages précédents -
Hi,
I get the following error message. Why? What's wrong with me? Thanks.

error C2039:'map' is not a member of 'std'
----------------
// .h file
#include <iostream>

template <int vdouble loc(size_t); // forward declarations of
function specializations (defined in .cxx file)
template <double loc<1>(size_t);
template <double loc<2>(size_t); // and so on
------------
#include <iostream>
#include "SomeClass.h"
using std::cout;
using std::endl;

int main()
{
std::map<size_t, double (*)(size_t)location;

location[1] = &loc<1>;
location[2] = &loc<2>;
cout<<"using loc<1>: "<<loc<1>(1)<<endl;

return 0;
}
Dec 30 '07 #11
On Dec 29, 7:56 pm, fl <rxjw...@gmail.comwrote:
On 29 déc, 15:09, aaragon <alejandro.ara...@gmail.comwrote:
On Dec 29, 10:52 am, "Daniel T." <danie...@earthlink.netwrote:
aaragon <alejandro.ara...@gmail.comwrote:
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.
Unless a single run is extremely fast, and you will be running this
program thousands of times per second, the extra time it take to
initialize all of the vectors is irrelevant. And if the run is extremely
fast and you are running the program thousands of times per second, then
you should be embedding this code in a program that makes several passes
per run.
So yes, it does make sense to initialize all of them.
Ok, so I decided to follow your advice but with a little twist.
Instead of having a map to a vector, I will have now a map to function
pointers. Inside those functions the arrays (known at compilation
time) are declared static so they're initialized only once (when I
call the function the first time). In this way, I don't need to
initialize what I won't use. Also, I don'tneed to have annoying switch
or if blocks. It looks like this:
// .h file
template <int v>
double loc(size_t);
// forward declarations of function specializations (defined in .cxx
file)
template <>
double loc<1>(size_t);
template <>
double loc<2>(size_t);
// and so on
template <
class MapPolicy = std::map<size_t, double (*)(size_t)>
struct Map {
typedef MapPolicy MapType;
MapType loc_;
Map() : loc_() {
// add location function pointers
loc_[1] = &loc<1>; // ERROR!!!
loc_[2] = &loc<2>; // ERROR!!!
// and so on
}
inline double loc(size_t i, size_t gp) {
return loc_[gp](i);
}
};
// .cxx file, partial specializations for the function
template<>
double loc<1>(size_t i) {
static const double loc[] = { 0.4 };
return loc[i];}
template<>
double loc<2>(size_t i) {
static const double loc[] = { 0.2, 0.6 };
return loc[i];}
// and so on until 30
Ok, this looks good and it's just what I want. However, for some
strange reason that I really don't see, I cannot compile this code. At
those ERROR lines I have the following message:
testMap.h: In constructor 'fea::Map<MapPolicy>::Map()':
testMap.h:36: error: expected primary-expression before ';' token
It doesn't allow me to take the address of the templated function. The
strange thing is that I can do the same from the main.cxx file, that
is:
// main.cxx
std::map<size_t, double (*)(size_t)location;
location[1] = &loc<1>;
location[2] = &loc<2>;
cout<<"what the -"<<location[2](1)<<endl;
works fine! Is this a problem with my compiler or there is something
wrong with the syntax? I'm using gcc version 4.1.2. I know, I know...
I could just use regular functions and call them loc1, loc2 and so on
and forget about the template (this will definitely work), but hey,
I'm learning day by day and I just cannot give up that easily, right?
a²- Masquer le texte des messages précédents -
- Afficher le texte des messages précédents -

Hi,
I get the following error message. Why? What's wrong with me? Thanks.

error C2039:'map' is not a member of 'std'
----------------
// .h file
#include <iostream>

template <int vdouble loc(size_t); // forward declarations of
function specializations (defined in .cxx file)
template <double loc<1>(size_t);
template <double loc<2>(size_t); // and so on
------------
#include <iostream>
#include "SomeClass.h"
using std::cout;
using std::endl;

int main()
{
std::map<size_t, double (*)(size_t)location;

location[1] = &loc<1>;
location[2] = &loc<2>;
cout<<"using loc<1>: "<<loc<1>(1)<<endl;

return 0;

}
You forgot to put 'using std::map;' or just put 'using namespace std;'
and don't forget to implement the actual loc<vfunctions in the cpp
file.


Dec 30 '07 #12
aaragon <al**************@gmail.comwrote:
The memory in your approach is the same (the executable
will have the same size) but the run time will be much less if I only
initialize what I need.
"More computing sins are committed in the name of efficiency (without
necessarily achieving it) than for any other single reason - including
blind stupidity." - W.A. Wulf

How much less is "much less"?
Dec 30 '07 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Kurt Van Campenhout | last post by:
Hi, I am trying to get/set Terminal server information in the active directory on a windows 2000 domain. Since the ADSI calls for TS don't work until W2K3, I need to do it myself. I'm fairly...
5
by: SStory | last post by:
Hi all, I really needed to get the icons associated with each file that I want to show in a listview. I used the follow modified code sniplets found on the internet. I have left in...
0
by: Pawan Narula via DotNetMonster.com | last post by:
hi all, i'm using VB.NET and trying to code for contact management in a tree. all my contacts r saved in a text file and my C dll reads them one by one and sends to VB callback in a sync mode...
3
by: msnews.microsoft.com | last post by:
Hi i am using User32.dll in Visual stdio 2005. public static extern long SetActiveWindow(long hwnd); public static extern long keybd_event(byte bVk, byte bScan, long dwFlags,
5
by: Lee | last post by:
(I also posted this query in Microsoft.Public.DotNet.Framework yesterday, but since I have received no responses, I am posting it here too.) Using Windows XP with all updates applied and Visual...
0
by: Yong | last post by:
I have a COM DLL written in C++ which worked fine when called from VB6. But we have now moved to Visual Studio 2005 - .NET Framework v2 and Windows XP Pro. In total there are 4 methods in this DLL...
2
by: Gillard | last post by:
hello I get a dll with standard call in C ++ but I really do not know how to use it in VB anyone can help??? there is the declarations in cpp to use the functions #ifndef SFPDF_H #define...
6
by: =?Utf-8?B?Sm9lbWFuYw==?= | last post by:
Hi, I'm trying to figure out the Callback method. I have a .dll that I call from a sub in my unmanaged code/calling program. That works just fine. But I'd like to have the .dll finish doing it's...
10
by: Matthias | last post by:
Dear newsgroup. I want to write a template function which accepts either integer or floating point numbers. If a certain result is not a whole number and if the template parameter is an...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.