Templates
-
I'm finding templates a bit hard to understand. I do understand some simple ones, but not everything. I looked at a sample program that finds max of two numbers and returns the bigger number. People say templates are generic but why not it take std::string into the function that finds of Max of two types(now it takes only numbers) We know that it's not sane to try "<" between strings but how does the compiler find the passed values are strings and reject it? and btw, how an STL container gets in anything we push? may it be numbers or strings, for example a vector. Can someone make a simple template program that just "holds" Object of any type, I just want to break the template barrier.
Box <int> box_int; box_int.hold(1); Box <std::string> box_str; box_str.hold("Astricks"); Box <myClass> box_obj; box_obj.hold(myObj);
Any help? Plz note: It's not a homework or assignement. -
I'm finding templates a bit hard to understand. I do understand some simple ones, but not everything. I looked at a sample program that finds max of two numbers and returns the bigger number. People say templates are generic but why not it take std::string into the function that finds of Max of two types(now it takes only numbers) We know that it's not sane to try "<" between strings but how does the compiler find the passed values are strings and reject it? and btw, how an STL container gets in anything we push? may it be numbers or strings, for example a vector. Can someone make a simple template program that just "holds" Object of any type, I just want to break the template barrier.
Box <int> box_int; box_int.hold(1); Box <std::string> box_str; box_str.hold("Astricks"); Box <myClass> box_obj; box_obj.hold(myObj);
Any help? Plz note: It's not a homework or assignement. -
I'm finding templates a bit hard to understand. I do understand some simple ones, but not everything. I looked at a sample program that finds max of two numbers and returns the bigger number. People say templates are generic but why not it take std::string into the function that finds of Max of two types(now it takes only numbers) We know that it's not sane to try "<" between strings but how does the compiler find the passed values are strings and reject it? and btw, how an STL container gets in anything we push? may it be numbers or strings, for example a vector. Can someone make a simple template program that just "holds" Object of any type, I just want to break the template barrier.
Box <int> box_int; box_int.hold(1); Box <std::string> box_str; box_str.hold("Astricks"); Box <myClass> box_obj; box_obj.hold(myObj);
Any help? Plz note: It's not a homework or assignement.(1) Maybe you are expecting to much from templates. They are just a blueprint for the compiler, how to make functions or classes using the same code for different types. (2) It *is* sane to use "<" to compare std::string - it overloads
operator <
What you mean is probably that < doesn't do a string comparison forchar *
- and in this case, you are right: the "normal" min() template won't worktemplate <typename T> T const & min(T const & a, T const & b) { return (a<b) ? a : b; }
This template works for all types that implement a "sensible"operator <
. It is generic for all typesthat fulfil this template parameter restriction In C++ you can even provide a specific implementaiton of a template function for a specific type. This is called template specialization:template<> char const * min(char const * a, char const * b) { return strcmp(a,b) < 0; }
(Note: this isn't a recommended solution for the problem at hand, but an illustration of template specialization) The template saves you from writing the samemin
function for dozens (or thousads) of types. It does not save you from defining what "min" actually means in terms of C++ (3) Generally, writing correct templates is hard, because you have to be very precise about your template argument restrictions, and you have to keep "all" types in mind. Used correctly, templates avoid a lot of repetetive code, and are fairly simple to use. (4) What you ask is simple, but probably not what you want:template <typename T>
class Box
{
T m_value;
public:
Box(T const & value) : m_value(value) {}
T const & get_value() const { retrun m_value; }
}However, you haven't broken any "template barrier": Box<string> is a completely different type from Box<int> there is no
Box
type that can hold both (Box
is just a template for types, not a type by itself) What you want isn't simple. boost::any[^] implements a container type that can hold all kinds of types. (5) Try to read more[^] on templates, try to unde -
(1) Maybe you are expecting to much from templates. They are just a blueprint for the compiler, how to make functions or classes using the same code for different types. (2) It *is* sane to use "<" to compare std::string - it overloads
operator <
What you mean is probably that < doesn't do a string comparison forchar *
- and in this case, you are right: the "normal" min() template won't worktemplate <typename T> T const & min(T const & a, T const & b) { return (a<b) ? a : b; }
This template works for all types that implement a "sensible"operator <
. It is generic for all typesthat fulfil this template parameter restriction In C++ you can even provide a specific implementaiton of a template function for a specific type. This is called template specialization:template<> char const * min(char const * a, char const * b) { return strcmp(a,b) < 0; }
(Note: this isn't a recommended solution for the problem at hand, but an illustration of template specialization) The template saves you from writing the samemin
function for dozens (or thousads) of types. It does not save you from defining what "min" actually means in terms of C++ (3) Generally, writing correct templates is hard, because you have to be very precise about your template argument restrictions, and you have to keep "all" types in mind. Used correctly, templates avoid a lot of repetetive code, and are fairly simple to use. (4) What you ask is simple, but probably not what you want:template <typename T>
class Box
{
T m_value;
public:
Box(T const & value) : m_value(value) {}
T const & get_value() const { retrun m_value; }
}However, you haven't broken any "template barrier": Box<string> is a completely different type from Box<int> there is no
Box
type that can hold both (Box
is just a template for types, not a type by itself) What you want isn't simple. boost::any[^] implements a container type that can hold all kinds of types. (5) Try to read more[^] on templates, try to unde -
(1) Maybe you are expecting to much from templates. They are just a blueprint for the compiler, how to make functions or classes using the same code for different types. (2) It *is* sane to use "<" to compare std::string - it overloads
operator <
What you mean is probably that < doesn't do a string comparison forchar *
- and in this case, you are right: the "normal" min() template won't worktemplate <typename T> T const & min(T const & a, T const & b) { return (a<b) ? a : b; }
This template works for all types that implement a "sensible"operator <
. It is generic for all typesthat fulfil this template parameter restriction In C++ you can even provide a specific implementaiton of a template function for a specific type. This is called template specialization:template<> char const * min(char const * a, char const * b) { return strcmp(a,b) < 0; }
(Note: this isn't a recommended solution for the problem at hand, but an illustration of template specialization) The template saves you from writing the samemin
function for dozens (or thousads) of types. It does not save you from defining what "min" actually means in terms of C++ (3) Generally, writing correct templates is hard, because you have to be very precise about your template argument restrictions, and you have to keep "all" types in mind. Used correctly, templates avoid a lot of repetetive code, and are fairly simple to use. (4) What you ask is simple, but probably not what you want:template <typename T>
class Box
{
T m_value;
public:
Box(T const & value) : m_value(value) {}
T const & get_value() const { retrun m_value; }
}However, you haven't broken any "template barrier": Box<string> is a completely different type from Box<int> there is no
Box
type that can hold both (Box
is just a template for types, not a type by itself) What you want isn't simple. boost::any[^] implements a container type that can hold all kinds of types. (5) Try to read more[^] on templates, try to undeYour example was so nice, realy useful. thanks a lot man. And I tried another simple thing, but why do I get this error peter?
template class Key
{
T k;
T* kptr;
int length;
public:
Key(T);
// ...
};void main()
{
Key < int > i;
}: error C2512: 'Key' : no appropriate default constructor available
*
-
Your example was so nice, realy useful. thanks a lot man. And I tried another simple thing, but why do I get this error peter?
template class Key
{
T k;
T* kptr;
int length;
public:
Key(T);
// ...
};void main()
{
Key < int > i;
}: error C2512: 'Key' : no appropriate default constructor available
*
If a coworker asked me that, I'd start with a "questions" game: What does the compiler say? Where does it saay that? etc. - you could work this out yourself ;) since you declare Key(T), the default constructor Key() is not generated automatically.
Developers, Developers, Developers, Developers, Developers, Developers, Velopers, Develprs, Developers!
We are a big screwed up dysfunctional psychotic happy family - some more screwed up, others more happy, but everybody's psychotic joint venture definition of CP
Linkify!|Fold With Us! -
If a coworker asked me that, I'd start with a "questions" game: What does the compiler say? Where does it saay that? etc. - you could work this out yourself ;) since you declare Key(T), the default constructor Key() is not generated automatically.
Developers, Developers, Developers, Developers, Developers, Developers, Velopers, Develprs, Developers!
We are a big screwed up dysfunctional psychotic happy family - some more screwed up, others more happy, but everybody's psychotic joint venture definition of CP
Linkify!|Fold With Us!oops, just a simple mistake. I'm taking up templates with an assumption it's too difficult to understand. So any error it throws look big for me . Sorry peter, it's the usual "no definition" error. Damn, I should have put Key(T){}. Now I defined it inline. Sorry for the stupid question. And Btw, next time if I come up with a good question would you try to extend your support? please?
*
-
oops, just a simple mistake. I'm taking up templates with an assumption it's too difficult to understand. So any error it throws look big for me . Sorry peter, it's the usual "no definition" error. Damn, I should have put Key(T){}. Now I defined it inline. Sorry for the stupid question. And Btw, next time if I come up with a good question would you try to extend your support? please?
*
No problem :) I just want to encourage you to help yourself.
Astricks wrote:
And Btw, next time if I come up with a good question would you try to extend your support? please?
No promise, but you aren't on a black list or something ;)
Developers, Developers, Developers, Developers, Developers, Developers, Velopers, Develprs, Developers!
We are a big screwed up dysfunctional psychotic happy family - some more screwed up, others more happy, but everybody's psychotic joint venture definition of CP
Linkify!|Fold With Us! -
No problem :) I just want to encourage you to help yourself.
Astricks wrote:
And Btw, next time if I come up with a good question would you try to extend your support? please?
No promise, but you aren't on a black list or something ;)
Developers, Developers, Developers, Developers, Developers, Developers, Velopers, Develprs, Developers!
We are a big screwed up dysfunctional psychotic happy family - some more screwed up, others more happy, but everybody's psychotic joint venture definition of CP
Linkify!|Fold With Us!