Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Elegant way to initialize non-static array member (2)

Reply
Thread Tools

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

 
 
mathieu
Guest
Posts: n/a
 
      11-08-2007
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;
}

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      11-08-2007
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


 
Reply With Quote
 
 
 
 
mathieu
Guest
Posts: n/a
 
      11-08-2007
On Nov 8, 4:24 pm, "Victor Bazarov" <(E-Mail Removed)> 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

 
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
elegant way of getting last REAL value of an array ccc31807 Perl Misc 5 07-19-2012 06:14 PM
best way to initialize static member objects cppaddict C++ 16 07-11-2012 11:28 AM
Most elegant way: array of files from array of directories Terry Michaels Ruby 4 10-16-2010 10:39 PM
Elegant way to initialize non-static array member mathieu C++ 13 11-10-2007 02:28 PM
How to initialize an array member in the member initialization list? jut_bit_zx@eyou.com C++ 3 10-10-2005 12:10 AM



Advertisments