473,412 Members | 1,873 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,412 software developers and data experts.

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

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,NmakeFunctor(T const (&a)[N])
{
Functor<T,Nf;
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,Nf = 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,Nf = v;

return 0;
}

Nov 8 '07 #1
2 1576
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,NmakeFunctor(T const (&a)[N])
{
Functor<T,Nf;
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,Nf = 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,Nf = 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
Nov 8 '07 #2
On Nov 8, 4:24 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
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,NmakeFunctor(T const (&a)[N])
{
Functor<T,Nf;
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,Nf = 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,Nf = 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

Nov 8 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Kamilche | last post by:
Is there a more elegant way to change the working directory of Python to the directory of the currently executing script, and add a folder called 'Shared' to the Python search path? This is what...
74
by: Peter | last post by:
Hi, So many times, I have seen compile warning: "you used a char* without initilize it", probably on the code like this: ------------ char* ptr; func(..., ptr); ----------
4
by: qazmlp | last post by:
// Test.C Line-300: namespace Line-301: { Line-302: std::vector<std::string> vecaNS ; Line-303: } The 'SUN Forte 7 C++ Compiler' reports the following warning for the above code:...
2
by: K. Culleton | last post by:
Don't install Windows XP SP2 if you use db2cctr.exe and have DB2 V7.1.0.6.0 installed. After I installed XP SP2, db2cctr.exe would not initialize. After I uninstalled SP2, db2cctr.exe worked...
19
by: moxm | last post by:
I have a statement declares a globle variable like this : char *pname = NULL; Then I used splint to check the code, I got errors: err.c:8:15: Global pname initialized to null value: pname =...
5
by: removeps-generic | last post by:
Hi. I'm using placement new to re-initialize part of an object. Is this OK? struct BaseImp { All& r_all; BaseImp(All& all) : r_all(all) }; struct Calc::Imp : public BaseImp
18
by: _dee | last post by:
Question about best use of interfaces: Say there's a 'Master' class that needs to implement a few interfaces: class Master : I1, I2, I3 { } The actual code already exists in smaller...
5
by: Licheng Fang | last post by:
I want to store Chinese in Unicode internally in my program, and give output in UTF-8 or GBK format. After two days of searching and reading, I still cannot find a simple and straightforward way to...
32
by: r.z. | last post by:
class vector3 { public: union { float data; struct { float x, y, z; };
9
by: void main | last post by:
I'm rather new to complex numbers in C and was wondering, how do I initialize a complex variable properly if the imaginary part is 0. I tried -------- #include <complex.h> float complex c...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.