#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>
class Foo final
{
public:
Foo() : i_{}
{
std::cout << "Default Constructor" << std::endl;
}
explicit Foo(const int i) : i_{i}
{
std::cout << "Parameterized Constructor: " << i_ << std::endl;
}
Foo(const Foo& other) : i_{other.i_}
{
std::cout << "Copy Constructor: " << i_ << std::endl;
}
Foo(Foo&& other) noexcept : i_{std::exchange(other.i_, 0)}
{
std::cout << "Move Constructor: " << i_ << "; other.i_: " << other.i_
<< std::endl;
}
Foo& operator=(const Foo& other)
{
i_ = other.i_;
std::cout << "Copy assignment operator: " << i_ << std::endl;
return *this;
}
Foo& operator=(Foo&& other) noexcept
{
std::swap(i_, other.i_);
std::cout << "Move assignment operator: " << i_ << "; other.i_: "
<< other.i_ << std::endl;
return *this;
}
const int& i() const noexcept
{
std::cout << "Getter" << std::endl;
return i_;
}
int& i()
{
std::cout << "Setter" << std::endl;
return i_;
}
~Foo() noexcept
{
std::cout << "Destructor" << std::endl;
}
private:
int i_;
};
int main()
{
std::cout << '1' << std::endl;
std::vector<Foo> foos{ Foo{1994} };
std::cout << '2' << std::endl;
std::for_each(foos.begin(), foos.end(),
[](Foo& foo)
{
foo.i() /= 2;
});
std::cout << '3' << std::endl;
for (const Foo& foo : foos)
{
std::cout << foo.i() << ' ';
}
std::cout << std::endl;
return 0;
}