ORIGINAL QUESTION
Is there any way to accomplish this short of defining a class for Nomnom instead, providing a one-arg constructor, and then providing the zero-arg and two-arg constructors that call the std::pair constructors?
You could have more constructor overloads as
ClassNoms() : one(std::make_pair(-1,"") {}
ClassNoms(int id) : one(std::make_pair(id,"") {}
ClassNoms(const std::string& name) : one(std::make_pair(-1,name) {}
ClassNoms(int id, const std::string& name) : one(std::make_pair(id,name) {}
UPDATE:
Is there any way to accomplish this short of defining a class for Nomnom
instead of the typedef
, providing a one-arg constructor, and then providing the zero-arg and two-arg constructors that call the std::pair
constructors?
typedef std::pair<int, std::string> Nomnom;
You can't provide a default constructor for your typedef
'd NomNom
, because std::pair<>
doesn't provide one. To achieve this, you'll need to either inherit a class NomNom
from std::pair<>
, or IMHO better provide a wrapper for it, and use this one:
template<typename First,typename Second>
struct DefaultValuedPair {
std::pair<First,Second> value;
First& first;
Second& second;
DefaultValuedPair
( const First& first = First()
, const Second& second = Second()
)
: value(std::make_pair(first,second))
, first(value.first)
, second(value.second) {}
DefaultValuedPair(const std::pair<First,Second>& rhs)
: value(rhs)
, first(value.first)
, second(value.second) {}
// Add more implicit type conversions for std::pair<First,Second>
// as necessary ...
};
And have
typedef DefaultValuedPair<int, std::string> Nomnom;
You could have a function that acts like a constructor, but it can't be named and called as pure equivalent for a NomNom
(declared) type, and this wouldn't give any advantage over using std::make_pair()
.
The proposed wrapper solution might have a huge impact on considerations of refactoring already existing code that uses NomNom
, and should work seamlessly with such. As mentioned, just replacing/disguising std::make_pair
per se, makes no sense IMHO.