Click here to Skip to main content
15,888,242 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionRe: user input in win32 application Pin
prasad_som25-Dec-06 18:56
prasad_som25-Dec-06 18:56 
Questionlittle dictionary Pin
rockfanskid25-Dec-06 16:44
rockfanskid25-Dec-06 16:44 
AnswerRe: little dictionary Pin
Hamid_RT26-Dec-06 7:06
Hamid_RT26-Dec-06 7:06 
QuestionMFC v.s. valarray Pin
ytod25-Dec-06 15:37
ytod25-Dec-06 15:37 
AnswerRe: MFC v.s. valarray Pin
prasad_som25-Dec-06 18:38
prasad_som25-Dec-06 18:38 
QuestionTemplates Pin
Astricks25-Dec-06 6:24
Astricks25-Dec-06 6:24 
AnswerRe: Templates Pin
markkuk25-Dec-06 10:12
markkuk25-Dec-06 10:12 
AnswerRe: Templates Pin
peterchen25-Dec-06 10:25
peterchen25-Dec-06 10:25 
(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 for char * - and in this case, you are right: the "normal" min() template won't work

template <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:
<br />
template<><br />
char const * min(char const * a, char const * b)<br />
{<br />
  return strcmp(a,b) < 0;<br />
}<br />


(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 same min 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 understand the common ground that these tutorials cover.

Good luck!



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!

GeneralRe: Templates Pin
Astricks25-Dec-06 19:04
Astricks25-Dec-06 19:04 
GeneralRe: Thanks, But another question, Pin
Astricks26-Dec-06 2:27
Astricks26-Dec-06 2:27 
GeneralRe: Thanks, But another question, Pin
peterchen26-Dec-06 2:38
peterchen26-Dec-06 2:38 
GeneralRe: Thanks, But another question, Pin
Astricks26-Dec-06 2:46
Astricks26-Dec-06 2:46 
GeneralRe: Thanks, But another question, Pin
peterchen26-Dec-06 3:15
peterchen26-Dec-06 3:15 
GeneralRe: Thanks, But another question, Pin
Astricks26-Dec-06 5:27
Astricks26-Dec-06 5:27 
QuestionWeird C4244 warning message Pin
Cristian Amarie25-Dec-06 5:24
Cristian Amarie25-Dec-06 5:24 
AnswerRe: Weird C4244 warning message Pin
Mark Salsbery25-Dec-06 6:32
Mark Salsbery25-Dec-06 6:32 
GeneralRe: Weird C4244 warning message Pin
Cristian Amarie25-Dec-06 7:16
Cristian Amarie25-Dec-06 7:16 
GeneralRe: Weird C4244 warning message Pin
Mark Salsbery25-Dec-06 10:08
Mark Salsbery25-Dec-06 10:08 
GeneralRe: Weird C4244 warning message Pin
Cristian Amarie25-Dec-06 19:39
Cristian Amarie25-Dec-06 19:39 
GeneralRe: Weird C4244 warning message Pin
Mark Salsbery26-Dec-06 5:18
Mark Salsbery26-Dec-06 5:18 
GeneralRe: Weird C4244 warning message Pin
Cristian Amarie26-Dec-06 6:12
Cristian Amarie26-Dec-06 6:12 
GeneralRe: Weird C4244 warning message Pin
prasad_som25-Dec-06 22:06
prasad_som25-Dec-06 22:06 
QuestionSubtracting times - need a result including milliseconds Pin
SWDevil25-Dec-06 5:11
SWDevil25-Dec-06 5:11 
AnswerRe: Subtracting times - need a result including milliseconds Pin
prasad_som25-Dec-06 19:41
prasad_som25-Dec-06 19:41 
QuestionMax. value of int type variable and unsigned int type variable Pin
cy163@hotmail.com25-Dec-06 3:31
cy163@hotmail.com25-Dec-06 3:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.