Please tell me how I can correct this piece of code so that the cookie_name_t class does not use a copy constructor and an assignment constructor.
When I do the following:
cookie_name(cookie_name const &) = delete;
cookie_name &operator=(cookie_name const &) = delete;
in the cookie_name_t class, the code not compiling.
Everything breaks down in the insert_or_update function.
class cookie_name final
{
public:
using value_type = std::string;
cookie_name() = delete;
// Copy constructor can throw, so no noexcept here
cookie_name(value_type const &value) noexcept(std::is_nothrow_copy_constructible_v<value_type>) : value_{value} {};
// Move constructor is noexcept
cookie_name(value_type &&value) noexcept(std::is_nothrow_move_constructible_v<value_type>) : value_{std::move(value)} {};
cookie_name(cookie_name const &) = default;
cookie_name &operator=(cookie_name const &) = default;
// Default move constructor and move assignment are noexcept
cookie_name(cookie_name &&) noexcept(std::is_nothrow_move_constructible_v<value_type>) = default; // for NRVO
cookie_name &operator=(cookie_name &&) noexcept(std::is_nothrow_move_assignable_v<value_type>) = default; // for NRVO
value_type operator()() const noexcept { return value_; } // for return value
// Comparison operators are noexcept
bool operator==(cookie_name const &rhs) const noexcept { return this->value_ == rhs.value_; }
bool operator!=(cookie_name const &rhs) const noexcept { return !(this->value_ == rhs.value_); }
private:
value_type value_;
};
using cookie_name_t = cookie_name;
using cookie_value_t = cookie_value;
class cookies final
{
public:
using cookie_t = std::tuple<cookie_name_t, cookie_value_t, cookie_options_t>;
private:
using raw_cookie_t = std::vector<cookie_t>;
public:
cookies() = default;
void cookie(cookie_name_t const &cookie, cookie_value_t const &value) { insert_or_update(std::make_tuple(cookie(), value(), std::string{})); }
auto write_cookies() const;
private:
void insert_or_update(cookie_t &&cookie);
private:
raw_cookie_t cookies_;
};
void cookies::insert_or_update(cookie_t &&cookie)
{
auto value{std::find_if(cookies_.begin(), cookies_.end(), [&cookie](auto &iter)
{ return get<0>(iter)() == get<0>(cookie)(); })};
if (value == cookies_.end())
cookies_.emplace_back(cookie);
else
*value = std::move(cookie);
}