MESSAGE
DATE | 2016-11-05 |
FROM | ruben safir
|
SUBJECT | Subject: [Learn] Fwd: templates within templates
|
From learn-bounces-at-nylxs.com Sat Nov 5 19:35:27 2016 Return-Path: X-Original-To: archive-at-mrbrklyn.com Delivered-To: archive-at-mrbrklyn.com Received: from www.mrbrklyn.com (www.mrbrklyn.com [96.57.23.82]) by mrbrklyn.com (Postfix) with ESMTP id DBDEB161313; Sat, 5 Nov 2016 19:35:26 -0400 (EDT) X-Original-To: learn-at-nylxs.com Delivered-To: learn-at-nylxs.com Received: from [10.0.0.62] (flatbush.mrbrklyn.com [10.0.0.62]) by mrbrklyn.com (Postfix) with ESMTP id 99259160E77 for ; Sat, 5 Nov 2016 19:35:23 -0400 (EDT) References: To: learn-at-nylxs.com From: ruben safir X-Forwarded-Message-Id: Message-ID: <776149bb-7ba3-3fcd-9d1d-9a0dc3c72069-at-mrbrklyn.com> Date: Sat, 5 Nov 2016 19:35:23 -0400 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.4.0 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/mixed; boundary="------------6A1624BA17889D132CF8E094" Subject: [Learn] Fwd: templates within templates X-BeenThere: learn-at-nylxs.com X-Mailman-Version: 2.1.17 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: learn-bounces-at-nylxs.com Sender: "Learn"
This is a multi-part message in MIME format. --------------6A1624BA17889D132CF8E094 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit
--------------6A1624BA17889D132CF8E094 Content-Type: message/rfc822; name="templates within templates.eml" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="templates within templates.eml"
Path: reader2.panix.com!panix!not-for-mail From: Popping mad Newsgroups: comp.lang.c++ Subject: templates within templates Date: Sat, 5 Nov 2016 06:15:54 +0000 (UTC) Organization: PANIX Public Access Internet and UNIX, NYC Message-ID: NNTP-Posting-Host: www.mrbrklyn.com Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Trace: reader2.panix.com 1478326554 12264 96.57.23.82 (5 Nov 2016 06:15:54 GMT) X-Complaints-To: abuse-at-panix.com NNTP-Posting-Date: Sat, 5 Nov 2016 06:15:54 +0000 (UTC) User-Agent: Pan/0.140 (Chocolate Salty Balls; GIT b8fc14e git.gnome.org/git/pan2) Xref: panix comp.lang.c++:1125018
I have a class that is a template class
template class state { public: /* ==================== LIFECYCLE ======================================= */ /* constructor */ explicit inline state(std::string const a, cost r)//r is the current minimal state for a node :_nam{a}, _r{r} {};
/* ==================== ACCESSORS ======================================= */ inline std::string nam(){return _nam;}; inline cost r(){return _r;}; //once it is set it is set //inline void nam(std::string a ){_nam = a;} inline void r(cost b ){_r = b;} /* ==================== MUTATORS ======================================= */
/* ==================== OPERATORS ======================================= */
/* ==================== DATA MEMBERS ======================================= */ protected:
private: std::string _nam; cost _r;
}; /* ----- end of class State ----- */
I want to use it for a vector type in another class. In order to do this, I seem to have to make the second class also a template, which doesn't seem right. But I have to knwo the unknow template type prior to constructing the vectors
/* * ===================================================================================== * Class: Pstates * Description: vector of all possible states * ===================================================================================== */
template class Pstates { public:
/* ==================== LIFECYCLE ======================================= */ inline explicit Pstates (std::vector< state > x) :_vstate{x}{}; /* constructor */ inline ~Pstates (){delete _vstate; }; /* destructor */
/* ==================== ACCESSORS ======================================= */
/* ==================== MUTATORS ======================================= */
/* ==================== OPERATORS ======================================= */
const Pstates& operator = ( const Pstates &other ); /* assignment operator */
/* ==================== DATA MEMBERS ======================================= */ protected:
private: std::vector > _vstate;
}; /* ----- end of class Pstates ----- */
Am I doing this wrong? And do I need to use Pstates in the constructor?
--------------6A1624BA17889D132CF8E094 Content-Type: message/rfc822; name="Re: templates within templates.eml" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="Re: templates within templates.eml"
Path: reader2.panix.com!panix!goblin2!goblin1!goblin.stu.neva.ru!border1.nntp.ams1.giganews.com!nntp.giganews.com!buffer1.nntp.ams1.giganews.com!news.giganews.com.POSTED!not-for-mail NNTP-Posting-Date: Sat, 05 Nov 2016 03:34:37 -0500 Date: Sat, 05 Nov 2016 10:34:33 +0200 From: Paavo Helde User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:25.8) Gecko/20151117 FossaMail/25.1.9 MIME-Version: 1.0 Newsgroups: comp.lang.c++ Subject: Re: templates within templates References: In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Message-ID: X-Usenet-Provider: http://www.giganews.com X-Trace: sv3-oEbt2+ErDhISW+3ftZNViY+UP9RhCGm2A2tdlQeVjhOBwHgCttoFJtnPZ771omCryXwJIqEqnqFGZzE!Tod9RJDGDOglsC/K+9L3iTIK6v8f4vFm9fBSY0orNxvNrcailGDlNimVABHFepdDGKU3j5jGHyM= X-Complaints-To: abuse-at-giganews.com X-DMCA-Notifications: http://www.giganews.com/info/dmca.html X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40 X-Original-Bytes: 5096 Xref: panix comp.lang.c++:1125019
On 5.11.2016 8:15, Popping mad wrote: > I have a class that is a template class > > template > class state > { > public: > /* ==================== LIFECYCLE ======================================= */ > /* constructor */ > explicit inline state(std::string const a, cost r)//r is the current minimal state for a node > :_nam{a}, _r{r} > {}; > > > /* ==================== ACCESSORS ======================================= */ > inline std::string nam(){return _nam;}; > inline cost r(){return _r;}; > //once it is set it is set > //inline void nam(std::string a ){_nam = a;} > inline void r(cost b ){_r = b;} > /* ==================== MUTATORS ======================================= */ > > /* ==================== OPERATORS ======================================= */ > > /* ==================== DATA MEMBERS ======================================= */ > protected: > > private: > std::string _nam; > cost _r; > > }; /* ----- end of class State ----- */
The 'inline' is redundant in this example, it is just adding noise.
Using 'cost' for a template type parameter seems disconcerting. To me, 'cost' is something which has e.g. value 12, not something which has a value like a type 'int'. Maybe it should be named smth like CostCalculationPolicy?
> > I want to use it for a vector type in another class. In order to do this, I seem to have to make the second class > also a template, which doesn't seem right. But I have to knwo the unknow template type prior to constructing the vectors
A template is not a class. Template is a compile-time prescription for making many classes. You cannot put templates into some concrete std::vector as the templates exist only at compile time and a concrete instance of std::vector exists only at run time.
Indeed, what you can do is to make another prescription (template) which prescribes how to use your abstract class template with another abstract class template (std::vector). It appears this is what you have.
Another option is to fix the template parameter, so that you get an instantiated actual class:
void foo() { std::vector> vstate; // vstate is an instance of a concrete class }
At some point one needs to fix the template types anyway, otherwise the program would not be able to do anything at run-time. It's up to you where and how to do that, in principle the whole program could be templates and main() could contain a single instantiation of them.
HTH Paavo
> > > > /* > * ===================================================================================== > * Class: Pstates > * Description: vector of all possible states > * ===================================================================================== > */ > > template > class Pstates > { > public: > > /* ==================== LIFECYCLE ======================================= */ > inline explicit Pstates (std::vector< state > x) > :_vstate{x}{}; /* constructor */ > inline ~Pstates (){delete _vstate; }; /* destructor */ > > /* ==================== ACCESSORS ======================================= */ > > /* ==================== MUTATORS ======================================= */ > > /* ==================== OPERATORS ======================================= */ > > const Pstates& operator = ( const Pstates &other ); /* assignment operator */ > > /* ==================== DATA MEMBERS ======================================= */ > protected: > > private: > std::vector > _vstate; > > }; /* ----- end of class Pstates ----- */ > > > > > Am I doing this wrong? And do I need to use Pstates in the constructor? >
--------------6A1624BA17889D132CF8E094 Content-Type: message/rfc822; name="Re: templates within templates.eml" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="Re: templates within templates.eml"
Path: reader2.panix.com!panix!not-for-mail From: Popping mad Newsgroups: comp.lang.c++ Subject: Re: templates within templates Date: Sat, 5 Nov 2016 14:20:11 +0000 (UTC) Organization: PANIX Public Access Internet and UNIX, NYC Message-ID: References: NNTP-Posting-Host: www.mrbrklyn.com Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Trace: reader2.panix.com 1478355611 13949 96.57.23.82 (5 Nov 2016 14:20:11 GMT) X-Complaints-To: abuse-at-panix.com NNTP-Posting-Date: Sat, 5 Nov 2016 14:20:11 +0000 (UTC) User-Agent: Pan/0.140 (Chocolate Salty Balls; GIT b8fc14e git.gnome.org/git/pan2) Xref: panix comp.lang.c++:1125024
On Sat, 05 Nov 2016 10:34:33 +0200, Paavo Helde wrote:
> The 'inline' is redundant in this example, it is just adding noise.
Thank you Paavo/ Why is it redundent here?
> > Using 'cost' for a template type parameter seems disconcerting. To me, > 'cost' is something which has e.g. value 12, not something which has a > value like a type 'int'.
Like a constant instead of a template value? Why do you say that? FWIW, this is supposed to eventually do the Fitch/Sankoff algorithm for Phylogenetics. I want to template it so that I can cook up different mechanism for cost determination on the same scafling of this work, and do some comparing. > Maybe it should be named smth like > CostCalculationPolicy? <== i really don't like long variable names. It is not really the policy though, but the numeric value. I'm starting with ints, but I can see possible needs for using floats later, or even get more flexible. I do see your point though.
Quote<making many classes. You cannot put templates into some concrete std::vector as the templates exist only at compile time and a concrete instance of std::vector exists only at run time.>>
What I thought would happen is that the compiler would substitute in the code, first for the inner template, and then for the outer one, but I suppose that would mean the g++ needs to do multiple passes in the compile, which it may, or may not do. vector is also a template so it is also in existence on at compile time?
Is that right? Is there really no creation of a datatype called vector on compilation? Are vectors run time embodiment in the stack, or on the heap
Quote<< Indeed, what you can do is to make another prescription (template) which prescribes how to use your abstract class template with another abstract class template (std::vector). It appears this is what you have.>>
Yeah I thought the compiler would pick that up without the explicit direction. This is a big limitation.
Quote <instantiated actual class:
void foo() { std::vector> vstate; // vstate is an instance of a concrete class } >>
I think you lose your flexibility with state in that case.
--------------6A1624BA17889D132CF8E094 Content-Type: message/rfc822; name="Re: templates within templates.eml" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="Re: templates within templates.eml"
Path: reader2.panix.com!panix!goblin1!goblin.stu.neva.ru!border1.nntp.ams1.giganews.com!nntp.giganews.com!buffer1.nntp.ams1.giganews.com!news.giganews.com.POSTED!not-for-mail NNTP-Posting-Date: Sat, 05 Nov 2016 10:03:36 -0500 Date: Sat, 05 Nov 2016 17:03:30 +0200 From: Paavo Helde User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:25.8) Gecko/20151117 FossaMail/25.1.9 MIME-Version: 1.0 Newsgroups: comp.lang.c++ Subject: Re: templates within templates References: In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Message-ID: X-Usenet-Provider: http://www.giganews.com X-Trace: sv3-Gro9ghN+JDNilZH+12087KYgHtTjxvV/dT2SdL8hTvwvKKhbU3/wn8tARuYSm748DeeQb5d3wwhGM/O!cNDvOXTAU7fIzkY+FjU+fW5UyKd2FDEqHk6wOfABn4Txwhc8vt+5Xm+bWAI+2pIu3/TNm9lytTM= X-Complaints-To: abuse-at-giganews.com X-DMCA-Notifications: http://www.giganews.com/info/dmca.html X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40 X-Original-Bytes: 4403 Xref: panix comp.lang.c++:1125026
On 5.11.2016 16:20, Popping mad wrote: > On Sat, 05 Nov 2016 10:34:33 +0200, Paavo Helde wrote: > >> The 'inline' is redundant in this example, it is just adding noise. > > Thank you Paavo/ Why is it redundent here?
If you define a member function inside the class definition, it is implicitly considered 'inline'.
>> >> Using 'cost' for a template type parameter seems disconcerting. To me, >> 'cost' is something which has e.g. value 12, not something which has a >> value like a type 'int'. > > Like a constant instead of a template value? Why do you say that? FWIW, this > is supposed to eventually do the Fitch/Sankoff algorithm for Phylogenetics. I want > to template it so that I can cook up different mechanism for cost determination > on the same scafling of this work, and do some comparing. > >> Maybe it should be named smth like >> CostCalculationPolicy? <== i really don't like long variable names. It is not > really the policy though, but the numeric value. I'm starting with ints, but I can > see possible needs for using floats later, or even get more flexible. I do see your > point though. > > Quote<> making many classes. You cannot put templates into some concrete > std::vector as the templates exist only at compile time and a concrete > instance of std::vector exists only at run time.>> > > What I thought would happen is that the compiler would substitute in the code, > first for the inner template, and then for the outer one, but I suppose that would > mean the g++ needs to do multiple passes in the compile, which it may, or may not do. > vector is also a template so it is also in existence on at compile time? > > Is that right? Is there really no creation of a datatype called vector on compilation? > Are vectors run time embodiment in the stack, or on the heap > > Quote<< Indeed, what you can do is to make another prescription (template) which > prescribes how to use your abstract class template with another abstract > class template (std::vector). It appears this is what you have.>> > > Yeah I thought the compiler would pick that up without the explicit direction. > This is a big limitation.
It looks like you have some conceptual confusion about what are C++ templates and when one can and should use them. Saying that there are "big limitations" is like saying that human ear has big limitations because it does not distinguish colors.
> > Quote > <> instantiated actual class: > > void foo() { > std::vector> vstate; > // vstate is an instance of a concrete class > } >>> > > I think you lose your flexibility with state in that case.
I see you snipped the most important part of my post where I mentioned that in some point in your code you must fix your types and instantiate the templates. If this does not suite your code, then maybe you should not use templates in the first place, but for example run-time polymorphism (derived classes and virtual functions).
hth Paavo
--------------6A1624BA17889D132CF8E094 Content-Type: message/rfc822; name="Re: templates within templates.eml" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="Re: templates within templates.eml"
Path: reader2.panix.com!panix!not-for-mail From: Popping mad Newsgroups: comp.lang.c++ Subject: Re: templates within templates Date: Sat, 5 Nov 2016 15:48:16 +0000 (UTC) Organization: PANIX Public Access Internet and UNIX, NYC Message-ID: References: NNTP-Posting-Host: www.mrbrklyn.com Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Trace: reader2.panix.com 1478360896 13949 96.57.23.82 (5 Nov 2016 15:48:16 GMT) X-Complaints-To: abuse-at-panix.com NNTP-Posting-Date: Sat, 5 Nov 2016 15:48:16 +0000 (UTC) User-Agent: Pan/0.140 (Chocolate Salty Balls; GIT b8fc14e git.gnome.org/git/pan2) Xref: panix comp.lang.c++:1125028
On Sat, 05 Nov 2016 17:03:30 +0200, Paavo Helde wrote:
>> I think you lose your flexibility with state in that case. > > I see you snipped the most important part of my post where I mentioned > that in some point in your code you must fix your types and instantiate > the templates. If this does not suite your code, then maybe you should > not use templates in the first place, but for example run-time > polymorphism (derived classes and virtual functions).
No slight was intended. Thank you for giving this your time. I'm not certain as to what your driving at, but obviously it is not what I thought you meant. Regardless, I will be using templates, and I need them for what I want to do, and polymophism and I'll likely avoid virtual functions at this time, although I will reevaluate things later.
Ruben
--------------6A1624BA17889D132CF8E094 Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline
_______________________________________________ Learn mailing list Learn-at-nylxs.com http://lists.mrbrklyn.com/mailman/listinfo/learn
--------------6A1624BA17889D132CF8E094--
|
|