473,387 Members | 1,497 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,387 software developers and data experts.

Elegant way to initialize non-static array member

Hi,

Consider the following (*). Is there a way to rewrite it so that it
remains convenient (N is being recomputed when array v is modified)
*and* compiles :)

Thanks,
-Mathieu

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

int main()
{
const double v[] = {0, 1, 4, 9, 16, 25, 36 };
const unsigned int N = sizeof(v) / sizeof(v[0]);
Functor<double,Nf = v; // will not compile
//Functor<double,7f = { {0, 1, 4, 9, 16, 25, 36 } }; // need to
compute 7 by hand...

return 0;
}

Nov 6 '07 #1
12 2117
On Nov 6, 3:32 pm, mathieu <mathieu.malate...@gmail.comwrote:
Hi,

Consider the following (*). Is there a way to rewrite it so that it
remains convenient (N is being recomputed when array v is modified)
*and* compiles :)

Thanks,
-Mathieu

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

};

int main()
{
const double v[] = {0, 1, 4, 9, 16, 25, 36 };
const unsigned int N = sizeof(v) / sizeof(v[0]);
Functor<double,Nf = v; // will not compile
//Functor<double,7f = { {0, 1, 4, 9, 16, 25, 36 } }; // need to
compute 7 by hand...

return 0;

}
Super-ugly solution:

#define V {0, 1, 4, 9, 16, 25, 36 }
int main()
{
const double v[] = V;
const unsigned int N = sizeof(v) / sizeof(v[0]);
Functor<double,Nf = { V };
....
}

Nov 6 '07 #2
mathieu wrote:
Hi,

Consider the following (*). Is there a way to rewrite it so that it
remains convenient (N is being recomputed when array v is modified)
*and* compiles :)

Thanks,
-Mathieu

(*)
template <typename T, unsigned int N>
struct Functor
{
T values[N];
};
Add this function:

template<class T, unsigned NFunctor<T,NmakeFunctor(T (&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 = v; // will not compile
//Functor<double,7f = { {0, 1, 4, 9, 16, 25, 36 } }; // need to
compute 7 by hand...
Functor<double,Nf = makeFunctor(v);
>
return 0;
}
Final 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;
}

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 6 '07 #3
On Nov 6, 3:51 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
mathieu wrote:
Hi,
Consider the following (*). Is there a way to rewrite it so that it
remains convenient (N is being recomputed when array v is modified)
*and* compiles :)
....
Final 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;
}
nice and simple !

Thanks,
-Mathieu

Nov 6 '07 #4
On Nov 6, 6:18 pm, mathieu <mathieu.malate...@gmail.comwrote:
On Nov 6, 3:51 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
mathieu wrote:
Hi,
Consider the following (*). Is there a way to rewrite it so that it
remains convenient (N is being recomputed when array v is modified)
*and* compiles :)

...


Final 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;
}

nice and simple !

Thanks,
-Mathieu- Hide quoted text -

- Show quoted text -
sorry to terminate, but this 'assigning to default constructed' not
initializing.
OP`s ugly solution is more efficient if 'T' has none-trivial
constructors.

regards,
FM.

Nov 6 '07 #5
On 6 Nov., 18:13, terminator <farid.mehr...@gmail.comwrote:
On Nov 6, 6:18 pm, mathieu <mathieu.malate...@gmail.comwrote:


On Nov 6, 3:51 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
mathieu wrote:
Hi,
Consider the following (*). Is there a way to rewrite it so that it
remains convenient (N is being recomputed when array v is modified)
*and* compiles :)
...
Final 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;
}
nice and simple !
Thanks,
-Mathieu- Hide quoted text -
- Show quoted text -

sorry to terminate, but this 'assigning to default constructed' not
initializing.
No. It is copyconstructing.
OP`s ugly solution is more efficient if 'T' has none-trivial
constructors.
No. Not if any modern (less than say 10 years old) C++ compiler is
used.
>
regards,
Nov 6 '07 #6
On Nov 6, 6:28 pm, peter koch <peter.koch.lar...@gmail.comwrote:
On 6 Nov., 18:13, terminator <farid.mehr...@gmail.comwrote:
On Nov 6, 6:18 pm, mathieu <mathieu.malate...@gmail.comwrote:
On Nov 6, 3:51 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
mathieu wrote:
Hi,
Consider the following (*). Is there a way to rewrite it so that it
remains convenient (N is being recomputed when array v is modified)
*and* compiles :)
...
Final 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;
}
nice and simple !
Thanks,
-Mathieu- Hide quoted text -
- Show quoted text -
sorry to terminate, but this 'assigning to default constructed' not
initializing.

No. It is copyconstructing.
OP`s ugly solution is more efficient if 'T' has none-trivial
constructors.

No. Not if any modern (less than say 10 years old) C++ compiler is
used.
Those kind of questions always kills me. I never learn assembly, and I
would have no way to know that std::copy is indeed doing the right
thing in this case. Any clue on how to see that ?

Thanks
-Mathieu

Nov 6 '07 #7
mathieu wrote:
On Nov 6, 6:28 pm, peter koch <peter.koch.lar...@gmail.comwrote:
>On 6 Nov., 18:13, terminator <farid.mehr...@gmail.comwrote:
[..]
>>OP`s ugly solution is more efficient if 'T' has none-trivial
constructors.

No. Not if any modern (less than say 10 years old) C++ compiler is
used.

Those kind of questions always kills me. I never learn assembly, and I
would have no way to know that std::copy is indeed doing the right
thing in this case. Any clue on how to see that ?
Not sure what you mean by "the right thing", but std::copy will most
likely do 'memcpy' on arrays of POD, which is usually quite efficient;
as to non-trivial constructors, I believe 'terminator' is referring to
the inefficiency due to assigning that 'std::copy' has to do. Although
there is no way to tell whether it's inefficient until it is measured
in a real-world situation.

The problem with the macro solution is the symbol 'V' you have to live
with in the program. It is global, it has no type, and if you have to
have several of those, you quickly pollute the translation unit with
those identifiers. It's more difficult to read as well (since the 'V'
macro lives separately from the places where it is used).

Theoretical inefficiency versus code maintainability... It's a common
issue with which software designers have to deal every day.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 6 '07 #8
On Nov 6, 4:51 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
[snip]
Final 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]);
-----------------------------------------------
Here Microsoft Visual C++ 2005 produces an error
Functor<double,Nf = makeFunctor(v);
-----------------------------------------------
>
return 0;
}
[snip]
Microsoft Visual C++ 2005 produces the following error
=========================
C2440: 'initializing' : cannot convert from 'Functor<T,N>' to
'Functor<T,N>'
with
[
T=const double,
N=7
]
and
[
T=double,
N=7
]
No user-defined-conversion operator available that can perform
this conversion, or the operator cannot be called
=============================================

Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn



Nov 7 '07 #9
Alex Vinokur wrote:
On Nov 6, 4:51 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
[snip]
>Final 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]);
-----------------------------------------------
Here Microsoft Visual C++ 2005 produces an error
> Functor<double,Nf = makeFunctor(v);
-----------------------------------------------
>>
return 0;
}
[snip]
Microsoft Visual C++ 2005 produces the following error
=========================
C2440: 'initializing' : cannot convert from 'Functor<T,N>' to
'Functor<T,N>'
with
[
T=const double,
N=7
]
and
[
T=double,
N=7
]
No user-defined-conversion operator available that can perform
this conversion, or the operator cannot be called
=============================================

Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn
Good to know.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 7 '07 #10
On Nov 7, 9:16 am, Alex Vinokur <ale...@users.sourceforge.netwrote:
On Nov 6, 4:51 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
[snip]
Final 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]);

-----------------------------------------------
Here Microsoft Visual C++ 2005 produces an error Functor<double,Nf = makeFunctor(v);

-----------------------------------------------
return 0;
}

[snip]

Microsoft Visual C++ 2005 produces the following error
=========================
C2440: 'initializing' : cannot convert from 'Functor<T,N>' to
'Functor<T,N>'
with
[
T=const double,
N=7
]
and
[
T=double,
N=7
]
No user-defined-conversion operator available that can perform
this conversion, or the operator cannot be called
=============================================
Why are not we going to treat intrinsic arrays as distinct coy
constructible types?
They can simply be fancied as some intrinsic template.

regards,
FM.

Nov 7 '07 #11
On Nov 7, 5:07 pm, terminator <farid.mehr...@gmail.comwrote:

[...]
Why are not we going to treat intrinsic arrays as distinct coy
constructible types?
Because that would break C compatibility.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Nov 8 '07 #12
On Nov 8, 1:27 pm, James Kanze <james.ka...@gmail.comwrote:
On Nov 7, 5:07 pm, terminator <farid.mehr...@gmail.comwrote:

[...]
Why are not we going to treat intrinsic arrays as distinct coy
constructible types?

Because that would break C compatibility.
including castability to appropariate pointers will remove any
incompatibilities .The internal interface of intrinsic array can be :

template <typename T, unsigned size_t n>
struct ___intrinsic_array{

___intrinsic_array(const ___intrinsic_array&);

typedef const T* ___CP;
typedef T* ___NP;
typedef volatile T* ___VP;

typedef const T& ___CR;
typedef T& ___NR;
typedef volatile T& ___VR;

inline operator ___CP()const
{return reinterpret_cast<___CP>(this);};
inline operator ___NP ()
{return reinterpret_cast<___NP >(this);};
inline operator ___VP()volatile
{return reinterpret_cast<___VP>(this);};

inline ___CR operator[](const size_t& i)const
{return *(operator ___CP()+i);};
inline ___NR operator[](const size_t& i)
{return *(operator ___NP()+i);};
inline ___VR operator[](const size_t& i)volatile
{return *(operator ___VP()+i);};

//assignment and construction can be defined as well.

};

regards,
FM.

Nov 10 '07 #13

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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.