M
ma740988
So I'm reading the C++ coding standards(Shutter & Andrei), more
specifically item 56. There's a statement:
"Prefer to provide a nonmember swap function in the same namespace as
your type when objects of your type have a way to exchange their values
more efficiently than via brute-force assignment, such as if they have
their own swap or equivalent function. Additionally, consider
specializing std::swap for your own template types:"
namespace std {
template <> void swap(MyType& lhs, MyType& rhs) {
lhs.swap(rhs);
}
}
-------- end shutter/andrei
So I'm trying to generate a case where to just that. Provide my own
'non member swap function'. So consider:
// test case
# include <iostream>
# include <string>
# include <algorith>
using namespace std;
class msg {
std::string msg_id;
size_t val;
public:
explicit msg(
std::string const& msg_id_,
int val_
)
: msg_id(msg_id_)
, val(val_) {}
msg& msg:perator=(const msg& m)
{
msg temp(m);
swap(temp);
return *this;
}
void swap(msg &rhs) {
msg_id.swap(rhs.msg_id);
std::swap(val, rhs.val);
}
};
namespace std {
template <> void swap(msg& lhs, msg& rhs) {
lhs.swap(rhs);
}
}
class my_class {
msg *my_msg;
public:
explicit my_class() {}
void do_a_special_thing(msg& msg_)
{
//my_msg = msg_;
}
};
int main()
{
my_class m;
m.do_a_special_thing(msg("hi there" , 5));
}
the function do_a_special_thing should assing msg_ to my_msg utilizing
my 'non member swap function', but there's something amiss about my
approach here so always appreaciate the help.
// formerly [[email protected]]
specifically item 56. There's a statement:
"Prefer to provide a nonmember swap function in the same namespace as
your type when objects of your type have a way to exchange their values
more efficiently than via brute-force assignment, such as if they have
their own swap or equivalent function. Additionally, consider
specializing std::swap for your own template types:"
namespace std {
template <> void swap(MyType& lhs, MyType& rhs) {
lhs.swap(rhs);
}
}
-------- end shutter/andrei
So I'm trying to generate a case where to just that. Provide my own
'non member swap function'. So consider:
// test case
# include <iostream>
# include <string>
# include <algorith>
using namespace std;
class msg {
std::string msg_id;
size_t val;
public:
explicit msg(
std::string const& msg_id_,
int val_
)
: msg_id(msg_id_)
, val(val_) {}
msg& msg:perator=(const msg& m)
{
msg temp(m);
swap(temp);
return *this;
}
void swap(msg &rhs) {
msg_id.swap(rhs.msg_id);
std::swap(val, rhs.val);
}
};
namespace std {
template <> void swap(msg& lhs, msg& rhs) {
lhs.swap(rhs);
}
}
class my_class {
msg *my_msg;
public:
explicit my_class() {}
void do_a_special_thing(msg& msg_)
{
//my_msg = msg_;
}
};
int main()
{
my_class m;
m.do_a_special_thing(msg("hi there" , 5));
}
the function do_a_special_thing should assing msg_ to my_msg utilizing
my 'non member swap function', but there's something amiss about my
approach here so always appreaciate the help.
// formerly [[email protected]]