I am implementing a class that uses Named Parameter Idiom to initialise its members:
class Person
{
public:
Person & first_name(std::string n) { m_first_name = n; return *this; }
Person & last_name(std::string l) { m_last_name = l; return *this; }
Person & age(int a) { m_age = a; return *this; }
std::string get_first_name() const { return m_first_name; }
std::string get_last_name() const { return m_last_name; }
int get_age() const { return m_age; }
private:
std::string m_first_name;
std::string m_last_name;
int m_age;
};
So far, everything works fine.
#include <string>
#include <iostream>
class Person
{
public:
Person & first_name(std::string n) { m_first_name = n; return *this; }
Person & last_name(std::string l) { m_last_name = l; return *this; }
Person & age(int a) { m_age = a; return *this; }
std::string get_first_name() const { return m_first_name; }
std::string get_last_name() const { return m_last_name; }
int get_age() const { return m_age; }
private:
std::string m_first_name;
std::string m_last_name;
int m_age;
};
void print(Person const & p)
{
std::cout << p.get_last_name() << ", " << p.get_first_name() << ", " << p.get_age() << '\n';
}
int main()
{
auto p1 = Person().first_name("Joe").age(20);
auto p2 = Person().last_name("Kane").age(50);
print(p1);
print(p2);
}
The above code outputs
, Joe, 20
Kane, , 50
as expected.
However, as soon as I make my class non-copyable (in my case by adding a std::unique_ptr
member), the above code fails to compile
#include <string>
#include <memory>
#include <iostream>
class Person
{
public:
Person & first_name(std::string n) { m_first_name = n; return *this; }
Person & last_name(std::string l) { m_last_name = l; return *this; }
Person & age(int a) { m_age = a; return *this; }
std::string get_first_name() const { return m_first_name; }
std::string get_last_name() const { return m_last_name; }
int get_age() const { return m_age; }
private:
std::string m_first_name;
std::string m_last_name;
int m_age;
std::unique_ptr<int> m_ptr; // added member
};
void print(Person const & p)
{
std::cout << p.get_last_name() << ", " << p.get_first_name() << ", " << p.get_age() << '\n';
}
int main()
{
auto p1 = Person().first_name("Joe").age(20);
auto p2 = Person().last_name("Kane").age(50);
print(p1);
print(p2);
}
with the following error from G++:
prog.cc: In function 'int main()':
prog.cc:30:48: error: use of deleted function 'Person::Person(const Person&)'
30 | auto p1 = Person().first_name("Joe").age(20);
| ^
prog.cc:5:7: note: 'Person::Person(const Person&)' is implicitly deleted because the default definition would be ill-formed:
5 | class Person
| ^~~~~~
prog.cc:5:7: error: use of deleted function 'std::unique_ptr<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = int; _Dp = std::default_delete<int>]'
In file included from /opt/wandbox/gcc-head/include/c++/10.0.0/memory:82,
from prog.cc:2:
/opt/wandbox/gcc-head/include/c++/10.0.0/bits/unique_ptr.h:456:7: note: declared here
456 | unique_ptr(const unique_ptr&) = delete;
| ^~~~~~~~~~
prog.cc:31:48: error: use of deleted function 'Person::Person(const Person&)'
31 | auto p2 = Person().last_name("Kane").age(50);
|
I fixed this by returning rvalues:
class Person
{
public:
Person && first_name(std::string n) { m_first_name = n; return std::move(*this); }
Person && last_name(std::string l) { m_last_name = l; return std::move(*this); }
Person && age(int a) { m_age = a; return std::move(*this); }
// ...
};
Is this the proper fix? Are there any pitfalls I should consider?