How do you create template array at compile-time?

I

Immortal Nephi

How do you create template array at compile-time? I pulled
information from http://www.parashift.com/c++-faq-lite/pointers-to-members.html
and the question is “[33.14] Can you make functionoids faster than
normal function calls?”
Notice blah function. Can you create functObj array in blah function
at compile-time?

class Funct1 {
public:
Funct1(float y) : y_(y) { }
int operator()(int x) { ...code from funct1... }

private:
float y_;
};

class Funct2 {
public:
Funct2(std::string const& y, int z) : y_(y), z_(z) { }
int operator()(int x) { ...code from funct2... }

private:
std::string y_;
int z_;
};

class Funct3 {
public:
Funct3(const std::vector<double>& y) : y_(y) { }
int operator()(int x) { ...code from funct3... }

private:
std::vector<double> y_;
};


template <typename FunctObj>
void myCode(FunctObj f)
{
// ...
f(...args-go-here...);
// ...
}

void blah()
{
// ...
Funct2 x("functionoids are powerful", 42);
myCode(x);
// ...
}
 
F

Francesco S. Carta

How do you create template array at compile-time? I pulled
information from http://www.parashift.com/c++-faq-lite/pointers-to-members.html
and the question is “[33.14] Can you make functionoids faster than
normal function calls?”
Notice blah function. Can you create functObj array in blah function
at compile-time?

In the following code "functObj" is the name given to the type parameter
of the "myCode" template function, and as such is not visible outside of
"myCode" - if you want to pass an array to that template, you must
change that template declaration and add a compile-time numeric
parameter that "sets to stone" the passed-to array length.

In any case, STL containers are there to solve such kind of
complications too - you can fill a std::vector (or std::list, std::set
or whatever) with functors or with functionoids and pass it to the
function or template function you want to.

Have I misunderstood your question? Should I post an example to
illustrate the points above?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top