2009/5/6 Ryan McConnehey <mccorywork@gmail.com>
I'm currently trying to fortify my class api by "make interfaces easy to use correctly and hard to use incorrectly".  I have a class as follows.

class api
{
  api(ConfigName const& configName_, PathName const& pathName_)
  : m_ConfigName(configName_.value)
  , m_PathName(pathName_.value)
  {}

  std::string   m_ConfigName;
  std::string   m_PathName;
};

Both ConfigName and PathName are typedef to a template structure as follows.

template<typename S>
struct ParameterValue
{
  ParameterValue(S value_) : value(value_) {}
  S value
};

typedef ParameterValue<std::string>   ConfigName;
typedef ParameterValue<std::string>   PathName;

I realized later that this method doesn't prevent the swapping of a ConfigName with a PathName parameter.  Since both parameters are typedef to the same "type" of ParameterValue they're interchangeable.  Is there a boost class, library or C++ idiom that would provide what I'm looking for?  Since I'm going to be using this concept for other classes I build, I'd like something that would easily create parameter classes to use but not be interchangeable with other classes of the same type.  I know that I could do this with a macro but I'm leaving that for a last resort.

Ryan

Try this:

template<typename S, int N>
struct ParameterValue
{
  ParameterValue(S value_) : value(value_) {}
  S value
};

typedef ParameterValue<std::string, 0>   ConfigName;
typedef ParameterValue<std::string, 1>   PathName;

Roman Perepelitsa.