Hi,
I want to use a different Konstruktor to initialize an array depending on
the type of objects stored. I guess it has something to do with accolator
traits, but I'm not quite sure how to handle. This is the situation:
template <typename T>
class B {
public:
B(int i) {
std::fill(t_, i, T() ) (!)
}
private:
T t_[10];
};
In the call of std::fill I want to have T() for all types except a few, for
which I would like to call T(1,1). How to manage this?
regards,
alex 4 2356
Alexander Stippler <st**@mathematik.uni-ulm.de> writes: Hi,
I want to use a different Konstruktor to initialize an array depending on the type of objects stored. I guess it has something to do with accolator traits, but I'm not quite sure how to handle. This is the situation:
template <typename T> class B { public: B(int i) { std::fill(t_, i, T() ) (!) } private: T t_[10]; };
In the call of std::fill I want to have T() for all types except a few, for which I would like to call T(1,1). How to manage this?
Specialization.
#include<algorithm>
template <typename T>
class B {
public:
B(int i) {
std::fill(t_, t_ + i, T() );
}
private:
T t_[10];
};
struct special_type
{
special_type():x(0),y(0){}
special_type(int x,int y):x(x),y(y){}
int x,y;
};
template<>
B<special_type>::B(int i){std::fill(t_, t_ + i, special_type(1,1));}
int main()
{
B<special_type> b(1);
}
llewelly wrote: Alexander Stippler <st**@mathematik.uni-ulm.de> writes:
Hi,
I want to use a different Konstruktor to initialize an array depending on the type of objects stored. I guess it has something to do with accolator traits, but I'm not quite sure how to handle. This is the situation:
template <typename T> class B { public: B(int i) { std::fill(t_, i, T() ) (!) } private: T t_[10]; };
In the call of std::fill I want to have T() for all types except a few, for which I would like to call T(1,1). How to manage this?
Specialization.
#include<algorithm>
template <typename T> class B { public: B(int i) { std::fill(t_, t_ + i, T() ); } private: T t_[10]; };
struct special_type { special_type():x(0),y(0){} special_type(int x,int y):x(x),y(y){} int x,y; };
template<> B<special_type>::B(int i){std::fill(t_, t_ + i, special_type(1,1));}
int main() { B<special_type> b(1); }
With respect to my question your solution is prefectly right. Unfortunately
I wasn't precise. I want to have a solution on class level and I would like
to be free in the choice of the constructor call in the initialization.
Thus I would like to have an additional template parameter (with default
value 'default constructor', which would provide the concrete constructor
to be used. I know that such things are possible, but I'm far away from
knowing how to realize it.
Regards,
alex
On Tue, 02 Sep 2003 16:06:19 +0200, Alexander Stippler
<st**@mathematik.uni-ulm.de> wrote: Hi,
I want to use a different Konstruktor to initialize an array depending on the type of objects stored. I guess it has something to do with accolator traits, but I'm not quite sure how to handle. This is the situation:
template <typename T> class B { public: B(int i) { std::fill(t_, i, T() ) (!) } private: T t_[10]; };
In the call of std::fill I want to have T() for all types except a few, for which I would like to call T(1,1). How to manage this?
template <class T>
struct default_creation_traits
{
static T default_value()
{
return T();
}
};
template <
typename T,
class CreationTraits = default_creation_traits<T>
class B
{
typedef CreationTraits creation_traits;
public:
B(int i) {
std::fill(t_, i, creation_traits::default_value());
}
private:
T t_[10];
};
template <class T>
struct special_creation_traits
{
static T default_value()
{
return T(1, 1);
}
};
Then, use
typedef B<mytype> mytypeB;
and
typedef B<myothertype, special_creation_traits<myothertype> >
myothertypeB;
or whatever. There are a number of approaches, depending on your exact
requirements, but the above is similar to the char_traits approach
used by basic_string.
Tom
tom_usenet wrote: template <class T> struct default_creation_traits { static T default_value() { return T(); } };
template < typename T, class CreationTraits = default_creation_traits<T> class B { typedef CreationTraits creation_traits; public: B(int i) { std::fill(t_, i, creation_traits::default_value()); } private: T t_[10]; };
template <class T> struct special_creation_traits { static T default_value() { return T(1, 1); } };
Then, use typedef B<mytype> mytypeB; and typedef B<myothertype, special_creation_traits<myothertype> > myothertypeB;
or whatever. There are a number of approaches, depending on your exact requirements, but the above is similar to the char_traits approach used by basic_string.
Tom
Thank you very much, Tom. It's exactly what I desired.
regards,
alex This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Donald 'Paddy' McCarthy |
last post by:
Hi,
I am having a few problems with a GUI. I am new to traits and wxGlade.
I have used wxGlade to create a Form with an embedded space for a
CustomWidget.
I have the traits demo and would like...
|
by: Mark A. Gibbs |
last post by:
Good day,
What is the safest way to get a non-end of file value?
char_traits<char_type>::eof() will get me eof, but how can I safely and
consistently generate a value that is not eof? should i...
|
by: sharmadeep1980 |
last post by:
Hi All,
I am facing a very unique problem while compling my project in
"Release" build.
The project is building in DEBUG mode but giving linking error on
Release build.
Here is the error:...
|
by: Ash |
last post by:
Hello everyone !
I am trying to find some sort of a cookbook or more examples for using
Enthought Traits to build GUI's. I tried to follow the documentations
present at the enthought site, but...
|
by: Bit Byte |
last post by:
Can't seem to get my head around the point of a trait class - no matter
how many times I read up on it - why not simply use functors or function
pointers ?
Anyone care to explain this in a...
|
by: Hong Ye |
last post by:
Traits is a useful template technique to simplfy the implementation of some
classes. however, I met some questions when I tried to understand and
implement it.
Following is an example of traits...
|
by: Colin J. Williams |
last post by:
Using >easy_install -v -f
http://code.enthought.com/enstaller/eggs/source
enthought.traits
The result is:
....
many lines
....
|
by: PeterAPIIT |
last post by:
Hello all C++ expert programmer,
i have wrote partial general allocator for my container.
After reading standard C++ library and code guru article, i have
several questions.
1. Why...
|
by: greek_bill |
last post by:
Hi,
I'm trying to develop a system where I can register some data/
information about a class. For example
// ClassInfo.h
template <class T>
struct ClassInfo
{
static const std::string ...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
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...
|
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,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
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: 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...
|
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...
| |