Click here to Skip to main content
15,892,298 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Using MFC With C++ Pin
BRIMID23-Nov-04 9:16
BRIMID23-Nov-04 9:16 
GeneralRe: Using MFC With C++ Pin
toxcct23-Nov-04 21:31
toxcct23-Nov-04 21:31 
GeneralRe: Using MFC With C++ Pin
David Crow23-Nov-04 9:18
David Crow23-Nov-04 9:18 
GeneralA strange error with CreateThread Pin
lillah23-Nov-04 7:17
lillah23-Nov-04 7:17 
GeneralRe: A strange error with CreateThread Pin
gamitech23-Nov-04 7:47
gamitech23-Nov-04 7:47 
GeneralRe: A strange error with CreateThread Pin
Blake Miller23-Nov-04 9:08
Blake Miller23-Nov-04 9:08 
Generaltraits? still don't get it Pin
...---...23-Nov-04 6:35
...---...23-Nov-04 6:35 
GeneralRe: traits? still don't get it Pin
Joaquín M López Muñoz23-Nov-04 10:33
Joaquín M López Muñoz23-Nov-04 10:33 
I'll try to explain what traits are with a simple example. Suppose you're writing a template function for advancing an iterator n positions:
template<typename Iterator>
Iterator advance(Iterator it,size_t n)
{
  for(size_t i=0;i<n;++i){
    ++it;
  }
  return it;
}
So far so good. After writing this, you promptly realize that advance can be implemented in a more efficient maner when the iterator is a pointer. You consult your C++ book about partial template specialization and write the following:
// specialization of advance for pointers
template<typename T>
Iterator advance(T* it,size_t n)
{
  return it+n;
}
So yo have the default implementation for any type of iterator and a fast implementation for pointers (of course, the fast solution is not applicable to every iterator type, since forward iterators can only be advanced one step at a time.) This is OK, but it is not perfect: some iterators, which are not pointers, could benefit of the fast implementation. Specializing advance for every random-access iterator you know of is not practical, since the user can provide her own iterators, and you want to remain as generic as possicle. What you'd really want to write is:
template<typename Iterator>
Iterator advance(Iterator it,size_t n)
{
  if(Iterator is random-access){
    return it+n;
  }
  else{
    for(size_t i=0;i<n;++i){
      ++it;
    }
    return it;
  }
}
This pseudocode cannot be easily be turned into real code. How do you express the "Iterator is random-access" constraint? A first shot at this is the following: suppose the iterators you're passed have a nested typedef whose only purpose is to tell you wether they are randon-access or not:
//tag types, whose only serve is to signal the kind of iterator
struct random_access_category{};
struct default_access_category{};
 
template<typename Iterator>
Iterator advance(Iterator it,size_t n)
{
  return advance_helper<typename Iterator::category>(it,n);
}
  
template<typename IteratorCategory,typename Iterator>
Iterator advance_helper(Iterator it,size_t n)
{
  // default impplementation
  for(size_t i=0;i<n;++i){
    ++it;
  }
  return it;
}
  
template<typename Iterator>
Iterator advance_helper<randon_access_category>(Iterator it,size_t n)
{
  // specialization for randon-acess iterators
  return it+n;
}
So, given an Iterator, its nested typedef Iterator::category can be used to dispatch to the most appropriate implementation of advance. But:
  • An iterator is not guaranteed to have such a category typedef, no matter how bad you need it.
  • Pointers cannot have a nested typedef
Here's where traits enter. Instead of requiring that an iterator type provides information about itself, we obtain such information from a separate class:
struct random_access_category{};
struct default_access_category{};
 
// traits class for describing an iterator type
// by default, an iterator is assummed *not* to be random-access
template <typename Iterator>
struct iterator_traits
{
  typedef default_access_category category;
};
 
// but we know pointers are randon-access
template <typename T>
struct iterator_traits<T*>
{
  typedef random_access_category category;
};
 
Iterator advance(Iterator it,size_t n)
{
  return advance_helper<typename iterator_traits<Iterator>::category>(it,n);
}
  
// the rest as before
This traits technique allows us to describe built-in types, as I've shiwn before, and also types written by someone else, without touching her source code:
#include "FastIteratorLib.h" // some third party library

template<typename T>
struct iterator_traits<fast_iterator_lib::fast_iterator<T> >
{
  typedef random_access_category category;
}
I hope you've gotten the idea. Our advance is now maximmaly efficient and can cope with built-in types and types from external libraries.

Well, this is more or less what traits are: class templates full of nested typedefs whose purpose is to desribe the properties of an external type, so that generic code can take advantage of this extra information.

Did this help?

Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
Want a Boost forum in Code Project? Vote here[^]!
GeneralRe: traits? still don't get it Pin
...---...24-Nov-04 10:17
...---...24-Nov-04 10:17 
GeneralMimicking system tray Pin
Diarrhio23-Nov-04 6:34
Diarrhio23-Nov-04 6:34 
GeneralRe: Mimicking system tray Pin
BaldwinMartin23-Nov-04 9:50
BaldwinMartin23-Nov-04 9:50 
GeneralRe: Mimicking system tray Pin
Diarrhio23-Nov-04 11:18
Diarrhio23-Nov-04 11:18 
GeneralRe: Mimicking system tray Pin
BaldwinMartin23-Nov-04 11:24
BaldwinMartin23-Nov-04 11:24 
GeneralUsing a DLL (Importing) Pin
sweep12323-Nov-04 6:21
sweep12323-Nov-04 6:21 
GeneralRe: Using a DLL (Importing) Pin
David Crow23-Nov-04 7:10
David Crow23-Nov-04 7:10 
GeneralRe: Using a DLL (Importing) Pin
BaldwinMartin23-Nov-04 9:41
BaldwinMartin23-Nov-04 9:41 
GeneralNon Static Images Pin
Anonymous23-Nov-04 5:20
Anonymous23-Nov-04 5:20 
GeneralRe: Non Static Images Pin
toxcct23-Nov-04 7:54
toxcct23-Nov-04 7:54 
GeneralParallel port and XP Pin
Budric B.23-Nov-04 4:39
Budric B.23-Nov-04 4:39 
GeneralRe: Parallel port and XP Pin
David Chamberlain23-Nov-04 9:20
David Chamberlain23-Nov-04 9:20 
QuestionHow can I use a toolbar to suspend my thread Pin
Anonymous23-Nov-04 4:24
Anonymous23-Nov-04 4:24 
AnswerRe: How can I use a toolbar to suspend my thread Pin
Budric B.23-Nov-04 4:47
Budric B.23-Nov-04 4:47 
GeneralRe: How can I use a toolbar to suspend my thread Pin
Antony M Kancidrowski23-Nov-04 4:51
Antony M Kancidrowski23-Nov-04 4:51 
GeneralRe: How can I use a toolbar to suspend my thread Pin
David Crow23-Nov-04 4:55
David Crow23-Nov-04 4:55 
GeneralRe: How can I use a toolbar to suspend my thread Pin
Antony M Kancidrowski23-Nov-04 5:04
Antony M Kancidrowski23-Nov-04 5:04 

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.