Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Template Parameter Question

Reply
Thread Tools

Template Parameter Question

 
 
Nephi Immortal
Guest
Posts: n/a
 
      03-11-2011
Please confirm if template definition is valid. It is the class
body. You may find the missing, but I don’t see the reason to add the
missing since C++ Compiler does not generate an error message. I
should have added A< T, T2 > in each member functions.
If I insert or remove template parameters on top of class, then I do
not need to modify each member functions. The alternative option is
to use typedef.
If I choose to explicit member function definitions outside the class
body, then I have to put template< typename ….> and class<…> on each
memer functions. Typedef is not helpful.

template< typename T, typename T2 >
class A {
public:
A( T t, T2 t2 ) : m_t( t ), m_t2( t2 ) {
}

~A() {
}

A( const A &r ) : m_t( r.m_t ), m_t2( r.m_t2 ) {
}

A &operator=( const A &right ) {
m_t = right.m_t;
m_t2 = right.m_t2;
return *this;
}

/*
A< T, T2 >( T t, T2 t2 ) : m_t( t ), m_t2( t2 ) {
}

~A< T, T2 > () {
}

A< T, T2 > ( const A< T, T2 > &r ) : m_t( r.m_t ), m_t2( r.m_t2 ) {
}

A< T, T2 > &operator=( const A< T, T2 > &right ) {
m_t = right.m_t;
m_t2 = right.m_t2;
return *this;
}
*/
private:
T m_t;
T2 m_t2;
};


int main() {
A< char, int > a( 1, 2 ), a2( 3, 4 );
a = a2;

return 0;
}
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      03-12-2011
On 3/11/2011 4:52 PM, Nephi Immortal wrote:
> Please confirm if template definition is valid. It is the class
> body. You may find the missing, but I don’t see the reason to add the
> missing since C++ Compiler does not generate an error message. I
> should have added A< T, T2> in each member functions.


What?!

The name of the class template ('A' in your case) is implicitly declared
in the class template definition, and means that type. For instance in
the definition of template<int a, class T> class Foo, the symbol 'Foo'
means the same thing as 'Foo<a,T>'.

If that's what you're asking, there is your answer. If it's not, make
yourself clearer.

> If I insert or remove template parameters on top of class, then I do
> not need to modify each member functions. The alternative option is
> to use typedef.


Do you have "C++ Templates" book by Vandevoorde and Josuttis? If yes,
use it. If not, get it, then use it.

> If I choose to explicit member function definitions outside the class
> body, then I have to put template< typename ….> and class<…> on each
> memer functions. Typedef is not helpful.
>
> template< typename T, typename T2>
> class A {
> public:
> A( T t, T2 t2 ) : m_t( t ), m_t2( t2 ) {
> }
>
> ~A() {
> }
>
> A( const A&r ) : m_t( r.m_t ), m_t2( r.m_t2 ) {
> }
>
> A&operator=( const A&right ) {
> m_t = right.m_t;
> m_t2 = right.m_t2;
> return *this;
> }
>
> /*
> A< T, T2>( T t, T2 t2 ) : m_t( t ), m_t2( t2 ) {
> }
>
> ~A< T, T2> () {
> }
>
> A< T, T2> ( const A< T, T2> &r ) : m_t( r.m_t ), m_t2( r.m_t2 ) {
> }
>
> A< T, T2> &operator=( const A< T, T2> &right ) {
> m_t = right.m_t;
> m_t2 = right.m_t2;
> return *this;
> }
> */
> private:
> T m_t;
> T2 m_t2;
> };
>
>
> int main() {
> A< char, int> a( 1, 2 ), a2( 3, 4 );
> a = a2;
>
> return 0;
> }


V
--
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
 
 
 
Nephi Immortal
Guest
Posts: n/a
 
      03-12-2011
On Mar 12, 8:35*am, Victor Bazarov <(E-Mail Removed)> wrote:
> On 3/11/2011 4:52 PM, Nephi Immortal wrote:
>
> > * *Please confirm if template definition is valid. *It is the class
> > body. *You may find the missing, but I don t see the reason to add the
> > missing since C++ Compiler does not generate an error message. *I
> > should have added A< *T, T2> *in each member functions.

>
> What?!
>
> The name of the class template ('A' in your case) is implicitly declared
> in the class template definition, and means that type. *For instance in
> the definition of template<int a, class T> class Foo, the symbol 'Foo'
> means the same thing as 'Foo<a,T>'.
>
> If that's what you're asking, there is your answer. *If it's not, make
> yourself clearer.
>
> > * *If I insert or remove template parameters on top of class, then I do
> > not need to modify each member functions. *The alternative option is
> > to use typedef.

>
> Do you have "C++ Templates" book by Vandevoorde and Josuttis? *If yes,
> use it. *If not, get it, then use it.
>
>
>
>
>
> > * *If I choose to explicit member function definitions outside the class
> > body, then I have to put template< *typename .> *and class< > *oneach
> > memer functions. *Typedef is not helpful.

>
> > template< *typename T, typename T2>
> > class A {
> > public:
> > * *A( T t, T2 t2 ) : m_t( t ), m_t2( t2 ) {
> > * *}

>
> > * *~A() {
> > * *}

>
> > * *A( const A&r ) : m_t( r.m_t ), m_t2( r.m_t2 ) {
> > * *}

>
> > * *A&operator=( const A&right ) {
> > * * * * * *m_t = right.m_t;
> > * * * * * *m_t2 = right.m_t2;
> > * * * * * *return *this;
> > * *}

>
> > /*
> > * *A< *T, T2>( T t, T2 t2 ) : m_t( t ), m_t2( t2 ) {
> > * *}

>
> > * *~A< *T, T2> *() {
> > * *}

>
> > * *A< *T, T2> *( const A< *T, T2> *&r ) : m_t( r.m_t ), m_t2( r.m_t2 ) {
> > * *}

>
> > * *A< *T, T2> *&operator=( const A< *T, T2> *&right ) {
> > * * * * * *m_t = right.m_t;
> > * * * * * *m_t2 = right.m_t2;
> > * * * * * *return *this;
> > * *}
> > */
> > private:
> > * *T m_t;
> > * *T2 m_t2;
> > };

>
> > int main() {
> > * *A< *char, int> *a( 1, 2 ), a2( 3, 4 );
> > * *a = a2;

>
> > * *return 0;
> > }


"C++ Templates" book by Vandevoorde and Josuttis

Ok. I will buy C++ Template book. You have recognized “How to
program C++” by Deitel provides a very little information about
template.

I want to learn how to write overloading operators by the following
technique rules including memory management and smart pointer. Also,
I can write vector and iterate from scratch for practical purpose
before I can start to use STL.. Which book do you recommend?
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      03-13-2011
On 3/12/2011 11:01 AM, Nephi Immortal wrote:
>[..]
> I want to learn how to write overloading operators by the following
> technique rules including memory management and smart pointer. Also,
> I can write vector and iterate from scratch for practical purpose
> before I can start to use STL.. Which book do you recommend?


C++ Templates by Vandevoorde and Josuttis, Modern C++ Design by
Alexandrescu.

And, honestly, I don't understand the need to "write vector and iterate
from scratch" before starting to use "STL". The Standard library is
there to be used, and in order to use it correctly you *don't need* to
write one from scratch yourself.

V
--
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
Nephi Immortal
Guest
Posts: n/a
 
      03-13-2011
On Mar 13, 8:40*am, Victor Bazarov <(E-Mail Removed)> wrote:
> On 3/12/2011 11:01 AM, Nephi Immortal wrote:
>
> >[..]
> > I want to learn how to write overloading operators by the following
> > technique rules including memory management and smart pointer. *Also,
> > I can write vector and iterate from scratch for practical purpose
> > before I can start to use STL.. *Which book do you recommend?

>
> C++ Templates by Vandevoorde and Josuttis, Modern C++ Design by
> Alexandrescu.
>
> And, honestly, I don't understand the need to "write vector and iterate
> from scratch" before starting to use "STL". *The Standard library is
> there to be used, and in order to use it correctly you *don't need* to
> write one from scratch yourself.


You asked a good question. Writing vector class from scratch help
you to learn how to write C++ code in a better design. It is just for
exercise practices as self-study. After you learn writing the code,
you become to be advanced programmer and joins with other programmers
to enhance better STL and Boost designs in the future.
You will have to code thousands of classes. Put them together to
become component or library or software development.
I focus studying the template and define better overloading operator
designs. I hope you understand what I mean.
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      03-14-2011
On 3/13/2011 5:41 PM, Nephi Immortal wrote:
> On Mar 13, 8:40 am, Victor Bazarov<(E-Mail Removed)> wrote:
>> On 3/12/2011 11:01 AM, Nephi Immortal wrote:
>>
>>> [..]
>>> I want to learn how to write overloading operators by the following
>>> technique rules including memory management and smart pointer. Also,
>>> I can write vector and iterate from scratch for practical purpose
>>> before I can start to use STL.. Which book do you recommend?

>>
>> C++ Templates by Vandevoorde and Josuttis, Modern C++ Design by
>> Alexandrescu.
>>
>> And, honestly, I don't understand the need to "write vector and iterate
>> from scratch" before starting to use "STL". The Standard library is
>> there to be used, and in order to use it correctly you *don't need* to
>> write one from scratch yourself.

>
> You asked a good question.


Actually, I didn't ask a question.

> Writing vector class from scratch help
> you to learn how to write C++ code in a better design. It is just for
> exercise practices as self-study. After you learn writing the code,
> you become to be advanced programmer and joins with other programmers
> to enhance better STL and Boost designs in the future.
> You will have to code thousands of classes. Put them together to
> become component or library or software development.


Where did this propaganda come from? Your boss or your professor? You
don't have to first learn metallurgy to become good at using a steel
hammer. You don't need to become a good gardener, a miller, or a
butcher to be a good cook. Snap out of it!

> I focus studying the template and define better overloading operator
> designs. I hope you understand what I mean.


No, I don't understand what you mean. Moreover, I don't think you
actually *mean* it. I think you're repeating some mantra you heard from
a person of authority, without actually thinking about it.

That's all.

V
--
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      03-14-2011
On Mar 13, 1:40 pm, Victor Bazarov <(E-Mail Removed)> wrote:
> On 3/12/2011 11:01 AM, Nephi Immortal wrote:


> >[..]
> > I want to learn how to write overloading operators by the following
> > technique rules including memory management and smart pointer. Also,
> > I can write vector and iterate from scratch for practical purpose
> > before I can start to use STL.. Which book do you recommend?


> C++ Templates by Vandevoorde and Josuttis, Modern C++ Design by
> Alexandrescu.


I'm not sure I'd cite those two books in the same sentence. The
Vandevoorde and Josuttis is a must if you want to develop
templates; there are few better books. (I don't know of any.)
The Alexandrescu is interesting if you want to explore just how
far you can go, but there's almost nothing in it that you would
want to use in production code: it's more a collection of
advanced ideas, to experiment with.

--
James Kanze
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      03-14-2011
On 3/14/2011 6:29 PM, James Kanze wrote:
> On Mar 13, 1:40 pm, Victor Bazarov<(E-Mail Removed)> wrote:
>> On 3/12/2011 11:01 AM, Nephi Immortal wrote:

>
>>> [..]
>>> I want to learn how to write overloading operators by the following
>>> technique rules including memory management and smart pointer. Also,
>>> I can write vector and iterate from scratch for practical purpose
>>> before I can start to use STL.. Which book do you recommend?

>
>> C++ Templates by Vandevoorde and Josuttis, Modern C++ Design by
>> Alexandrescu.

>
> I'm not sure I'd cite those two books in the same sentence. The
> Vandevoorde and Josuttis is a must if you want to develop
> templates; there are few better books. (I don't know of any.)
> The Alexandrescu is interesting if you want to explore just how
> far you can go, but there's almost nothing in it that you would
> want to use in production code: it's more a collection of
> advanced ideas, to experiment with.


And that's why they complement each other so well! Why would I
recommend books that are interchangeable? <shrug>

V
--
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
Paul
Guest
Posts: n/a
 
      03-24-2011

"Nephi Immortal" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
On Mar 13, 8:40 am, Victor Bazarov <(E-Mail Removed)> wrote:
>You asked a good question. Writing vector class from scratch help
>you to learn how to write C++ code in a better design. It is just for
>exercise practices as self-study. After you learn writing the code,
>you become to be advanced programmer and joins with other programmers
>to enhance better STL and Boost designs in the future.
>You will have to code thousands of classes. Put them together to
>become component or library or software development.
>I focus studying the template and define better overloading operator
>designs. I hope you understand what I mean.



I understand what you mean and I agree with you .
A really good blacksmith knows how to make a good hammer.



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
decltype as a template parameter when containing reference to anothertemplate parameter. Isti C++ 2 04-19-2010 10:01 PM
Using declaration inside first template parameter as default valuefor second template parameter. Stuart Redmann C++ 5 12-14-2007 08:42 AM
Template friend can't be template parameter Gianni Mariani C++ 1 09-05-2003 09:15 AM
Re: A Newbie Question about template template template tom_usenet C++ 0 07-24-2003 12:06 PM
Re: A Newbie Question about template template template Chris Theis C++ 2 07-24-2003 09:42 AM



Advertisments