Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

templates within templates

12 views
Skip to first unread message

Popping mad

unread,
Nov 5, 2016, 2:16:07 AM11/5/16
to
I have a class that is a template class

template<class cost>
class state
{
public:
/* ==================== LIFECYCLE ======================================= */
/* constructor */
explicit inline state<cost>(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 cost>
class Pstates
{
public:

/* ==================== LIFECYCLE ======================================= */
inline explicit Pstates (std::vector< state<cost> > 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<state <cost> > _vstate;

}; /* ----- end of class Pstates ----- */




Am I doing this wrong? And do I need to use Pstates<cost> in the constructor?

Paavo Helde

unread,
Nov 5, 2016, 4:34:49 AM11/5/16
to
On 5.11.2016 8:15, Popping mad wrote:
> I have a class that is a template class
>
> template<class cost>
> class state
> {
> public:
> /* ==================== LIFECYCLE ======================================= */
> /* constructor */
> explicit inline state<cost>(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<state<int>> 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

Popping mad

unread,
Nov 5, 2016, 10:20:25 AM11/5/16
to
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<<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.>>

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
<<Another option is to fix the template parameter, so that you get an
instantiated actual class:

void foo() {
std::vector<state<int>> vstate;
// vstate is an instance of a concrete class
}
>>

I think you lose your flexibility with state in that case.


Paavo Helde

unread,
Nov 5, 2016, 11:03:47 AM11/5/16
to
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'.
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
> <<Another option is to fix the template parameter, so that you get an
> instantiated actual class:
>
> void foo() {
> std::vector<state<int>> 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




Popping mad

unread,
Nov 5, 2016, 11:48:26 AM11/5/16
to
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
0 new messages