Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > How do you create template array at compile-time?

Reply
Thread Tools

How do you create template array at compile-time?

 
 
Immortal Nephi
Guest
Posts: n/a
 
      07-09-2010
How do you create template array at compile-time? I pulled
information from http://www.parashift.com/c++-faq-lit...o-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);
// ...
}
 
Reply With Quote
 
 
 
 
Francesco S. Carta
Guest
Posts: n/a
 
      07-09-2010
Immortal Nephi <(E-Mail Removed)>, on 08/07/2010 19:45:16, wrote:

> How do you create template array at compile-time? I pulled
> information from http://www.parashift.com/c++-faq-lit...o-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?

> 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);
> // ...
> }



--
FSC - http://userscripts.org/scripts/show/59948
http://fscode.altervista.org - http://sardinias.com
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
template template arguments: expected a class template, got `Component<T1, T2, T3> gary.bernstein@gmail.com C++ 1 06-08-2007 07:10 AM
Can you create an array of arraynames and then print the array contents ? paul.porcelli@scottishwater.co.uk Perl Misc 3 09-08-2005 12:41 PM
Re: A Newbie Question about template template template tom_usenet C++ 0 07-24-2003 12:06 PM
Re: A Newbie Question about template template template Chris Theis C++ 2 07-24-2003 09:42 AM



Advertisments