How do you create template array at compile-time?

Discussion in 'C++' started by Immortal Nephi, Jul 9, 2010.

  1. 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);
    // ...
    }
     
    Immortal Nephi, Jul 9, 2010
    #1
    1. Advertising

  2. Immortal Nephi <>, 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-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?

    > 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
     
    Francesco S. Carta, Jul 9, 2010
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. flamesrock
    Replies:
    8
    Views:
    486
    Hendrik van Rooyen
    Nov 24, 2006
  2. Nagaraj
    Replies:
    1
    Views:
    883
    Lionel B
    Mar 1, 2007
  3. Carter
    Replies:
    2
    Views:
    512
    Carter
    Mar 4, 2009
  4. Immortal Nephi
    Replies:
    3
    Views:
    676
    James Kanze
    Dec 23, 2009
  5. Replies:
    3
    Views:
    134
    Dave Weaver
    Sep 8, 2005
Loading...

Share This Page