 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Maitre Bart Guest
|
Posted: Wed Nov 15, 2006 6:35 am Post subject: template no-match |
|
|
I made a functor that outputs a custom-formatted string given a pair as
input:
class pair2string
{
public:
pair2string() : _sep(" => ") {}
pair2string(const string& sep_) : _sep(sep_) {}
template <typename F, typename S>
string operator()(pair<F,S>& p_) const
{
ostringstream oss;
oss << p_.first << _sep << p_.second;
return oss.str();
}
private:
const string _sep;
};
It works fine for:
cout << pair2string()(*m.begin()) << "\n";
where m is a map<string,int> .
But if one of the pair arguments is a pair, there is a no-match, as
for:
cout << pair2string()(make_pair("test",*m.begin())) << "\n";
So, I tried to add this overload in my class to solve the no-match:
template <typename F, typename SF, typename SS>
string operator()(pair<F,pair<SF,SS> >& p_) const
{
ostringstream oss;
oss << p_.first << _sep << pair2string(_sep)(p_.second);
return oss.str();
}
But I still get a no-match from the compiler. Anyone has any idea why
and have a solution to the no-match?
In the end, I'd like to overload the 2 other possibilities:
template <typename FF, typename FS>
string operator()(pair<pair<FF,FS>,S>& p_) const
{
ostringstream oss;
oss << pair2string(_sep)(p_.first) << _sep << p_.second;
return oss.str();
}
template <typename FF, typename FS, typename SF, typename SS>
string operator()(pair<pair<FF,FS>,pair<SF,SS> >& p_) const
{
ostringstream oss;
oss << pair2string(_sep)(p_.first) << _sep <<
pair2string(_sep)(p_.second);
return oss.str();
}
Thank you for helping me.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
dasjotre Guest
|
Posted: Thu Nov 16, 2006 3:55 am Post subject: Re: template no-match |
|
|
Maitre Bart wrote:
| Quote: | I made a functor that outputs a custom-formatted string given a pair as
input:
class pair2string
{
public:
pair2string() : _sep(" => ") {}
pair2string(const string& sep_) : _sep(sep_) {}
template <typename F, typename S
string operator()(pair<F,S>& p_) const
{
ostringstream oss;
oss << p_.first << _sep << p_.second;
return oss.str();
}
private:
const string _sep;
};
It works fine for:
cout << pair2string()(*m.begin()) << "\n";
where m is a map<string,int> .
But if one of the pair arguments is a pair, there is a no-match, as
for:
cout << pair2string()(make_pair("test",*m.begin())) << "\n";
|
first problem is that your pair2string::operator() takes a
non const and make_pair returns temporary which can only be
bound to a const reference
second problem is that you don't have an operator where
F or S can be pairs and there is no ostream << specialization
for pairs
| Quote: |
So, I tried to add this overload in my class to solve the no-match:
template <typename F, typename SF, typename SS
string operator()(pair<F,pair<SF,SS> >& p_) const
{
ostringstream oss;
oss << p_.first << _sep << pair2string(_sep)(p_.second);
return oss.str();
}
But I still get a no-match from the compiler. Anyone has any idea why
and have a solution to the no-match?
In the end, I'd like to overload the 2 other possibilities:
template <typename FF, typename FS
string operator()(pair<pair<FF,FS>,S>& p_) const
{
ostringstream oss;
oss << pair2string(_sep)(p_.first) << _sep << p_.second;
return oss.str();
}
template <typename FF, typename FS, typename SF, typename SS
string operator()(pair<pair<FF,FS>,pair<SF,SS> >& p_) const
{
ostringstream oss;
oss << pair2string(_sep)(p_.first) << _sep
pair2string(_sep)(p_.second);
return oss.str();
}
|
the short version:
(if you have a ostream<< defined for ::std::string)
class pair2string
{
public:
pair2string() : _sep(" => ") {}
pair2string(const ::std::string & sep_) : _sep(sep_) {}
template <typename T>
::std::string operator()(T const & p_) const
{
::std::ostringstream oss;
oss << p_;
return oss.str();
}
template <typename F, typename S>
::std::string operator()(::std::pair<F,S> const & p_) const
{
::std::ostringstream oss;
pair2string p2s(_sep);
oss << p2s(p_.first) << _sep << p2s(p_.second);
return oss.str();
}
private:
const ::std::string _sep;
};
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|