Elegant way to initialize non-static array member (2)

Discussion in 'C++' started by mathieu, Nov 8, 2007.

  1. mathieu

    mathieu Guest

    Hi there,

    I was recently help for an issue with the following code:

    template <typename T, unsigned int N>
    struct Functor
    {
    T values[N];
    };

    #include <algorithm>
    template <typename T, unsigned int N>
    Functor<T,N> makeFunctor(T const (&a)[N])
    {
    Functor<T,N> f;
    std::copy(a, a + N, f.values);
    return f;
    }

    int main()
    {
    const double v[] = {0, 1, 4, 9, 16, 25, 36 };
    const unsigned int N = sizeof(v) / sizeof(v[0]);

    Functor<double,N> f = makeFunctor(v);

    return 0;
    }


    Is this completely equivalent to doing (*). or am I garantee that
    template will be inlined (without explicit inline keyword).

    Thanks
    -Mathieu

    (*)
    #include <algorithm> // std::copy

    template <typename T, unsigned int N>
    struct Functor
    {
    Functor(T const (&a)[N])
    {
    std::copy(a, a+N, values);
    }

    T values[N];
    };

    int main()
    {
    const double v[] = {0, 1, 4, 9, 16, 25, 36 };
    const unsigned int N = sizeof(v) / sizeof(*v);
    Functor<double,N> f = v;

    return 0;
    }
     
    mathieu, Nov 8, 2007
    #1
    1. Advertising

  2. mathieu wrote:
    > Hi there,
    >
    > I was recently help for an issue with the following code:
    >
    > template <typename T, unsigned int N>
    > struct Functor
    > {
    > T values[N];
    > };
    >
    > #include <algorithm>
    > template <typename T, unsigned int N>
    > Functor<T,N> makeFunctor(T const (&a)[N])
    > {
    > Functor<T,N> f;
    > std::copy(a, a + N, f.values);
    > return f;
    > }
    >
    > int main()
    > {
    > const double v[] = {0, 1, 4, 9, 16, 25, 36 };
    > const unsigned int N = sizeof(v) / sizeof(v[0]);
    >
    > Functor<double,N> f = makeFunctor(v);
    >
    > return 0;
    > }
    >
    >
    > Is this completely equivalent to doing (*). or am I garantee that
    > template will be inlined (without explicit inline keyword).
    >
    > Thanks
    > -Mathieu
    >
    > (*)
    > #include <algorithm> // std::copy
    >
    > template <typename T, unsigned int N>
    > struct Functor
    > {
    > Functor(T const (&a)[N])
    > {
    > std::copy(a, a+N, values);
    > }
    >
    > T values[N];
    > };
    >
    > int main()
    > {
    > const double v[] = {0, 1, 4, 9, 16, 25, 36 };
    > const unsigned int N = sizeof(v) / sizeof(*v);
    > Functor<double,N> f = v;
    >
    > return 0;
    > }


    No, it's not. You had a POD class, and if you add a user-defined
    constructor to it, 'Functor' stops being a POD class. That was
    the only reason I suggested a stand-alone function.

    If you don't care for 'Functor' to be a POD class, the constructor
    solution is better.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Nov 8, 2007
    #2
    1. Advertising

  3. mathieu

    mathieu Guest

    On Nov 8, 4:24 pm, "Victor Bazarov" <> wrote:
    > mathieu wrote:
    > > Hi there,

    >
    > > I was recently help for an issue with the following code:

    >
    > > template <typename T, unsigned int N>
    > > struct Functor
    > > {
    > > T values[N];
    > > };

    >
    > > #include <algorithm>
    > > template <typename T, unsigned int N>
    > > Functor<T,N> makeFunctor(T const (&a)[N])
    > > {
    > > Functor<T,N> f;
    > > std::copy(a, a + N, f.values);
    > > return f;
    > > }

    >
    > > int main()
    > > {
    > > const double v[] = {0, 1, 4, 9, 16, 25, 36 };
    > > const unsigned int N = sizeof(v) / sizeof(v[0]);

    >
    > > Functor<double,N> f = makeFunctor(v);

    >
    > > return 0;
    > > }

    >
    > > Is this completely equivalent to doing (*). or am I garantee that
    > > template will be inlined (without explicit inline keyword).

    >
    > > Thanks
    > > -Mathieu

    >
    > > (*)
    > > #include <algorithm> // std::copy

    >
    > > template <typename T, unsigned int N>
    > > struct Functor
    > > {
    > > Functor(T const (&a)[N])
    > > {
    > > std::copy(a, a+N, values);
    > > }

    >
    > > T values[N];
    > > };

    >
    > > int main()
    > > {
    > > const double v[] = {0, 1, 4, 9, 16, 25, 36 };
    > > const unsigned int N = sizeof(v) / sizeof(*v);
    > > Functor<double,N> f = v;

    >
    > > return 0;
    > > }

    >
    > No, it's not. You had a POD class, and if you add a user-defined
    > constructor to it, 'Functor' stops being a POD class. That was
    > the only reason I suggested a stand-alone function.
    >
    > If you don't care for 'Functor' to be a POD class, the constructor
    > solution is better.


    Ok thanks. As for the issue with the 'return' in your makeFunctor I
    was told that c++ compiler can implement "Named Return Value
    Optimization" (NRVO) to avoid the extra copy.

    -Mathieu
     
    mathieu, Nov 8, 2007
    #3
    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. cppaddict
    Replies:
    16
    Views:
    19,932
    m_pahlevanzadeh
    Jul 11, 2012
  2. Replies:
    3
    Views:
    893
    Peter_Julian
    Oct 10, 2005
  3. mathieu
    Replies:
    13
    Views:
    849
    terminator
    Nov 10, 2007
  4. dolphin
    Replies:
    3
    Views:
    1,404
    Pete Becker
    Dec 5, 2007
  5. Terry Michaels
    Replies:
    4
    Views:
    181
    w_a_x_man
    Oct 16, 2010
Loading...

Share This Page