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=B2- Masquer le texte des messages pr=E9c=E9dents -
- Afficher le texte des messages pr=E9c=E9dents -
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.
Hm. Although it is true that those functions that you don't execute don't
initialize arrays, it is still the case that the executable will contain
all the data that would be needed to initialize the arrays (after all, the=
compiler cannot predict which array will be used at run-time). The actual
initialization of the array is probably a null-op since the compiler
already made the array reside in the data segment of the executable.
In order to access the one array that you actually need, the map will
perform a search that is going to use roughly 5 comparisons (given that
there are about 30 function pointers).
I would consider _faking_ the existence of several arrays:
struct double2d {
static
double const * begin ( unsigned int i ) {
static const double d [] =
{
0.1,
0.2, 0.3, 0.4,
0.2, 0.5
};
static const unsigned arr [] =
{ 0, 1, 4, 6 };
return ( &d[0] + arr[i] );
}
static
double const * end ( unsigned int i ) {
return ( begin(i+1) );
}
static
unsigned int size ( void ) {
return ( 3 );
}
};
#include <iostream>
#include <algorithm>
#include <iterator>
int main ( void ) {
for ( unsigned int i = 0; i < double2d::size(); ++i ) {
std::copy( double2d::begin(i), double2d::end(i),
std::ostream_iterator<double>( std::cout, " " ) );
std::cout << '\n';
}
}
Best
Kai-Uwe Bux
Nope, that doesn't work for me. Imagine that you have to expand that
has 30. It can be done, but why initialize the entire thing if it
initialize what I need.
to remove the constructor initialization. Why? Well, I thought it
function, that is, to make that function also static.