Ошибка C ++ C2280 - попытка ссылки на удаленную функцию - для примитивных типов - PullRequest
1 голос
/ 25 мая 2020

Я реализую свои две неупорядоченные карты, одна из которых принимает ключ, представляющий собой кортеж с 3 аргументами, а другая - кортеж с 2 аргументами. Вот мой код:

#pragma once

#include <boost/functional/hash.hpp>

#include <unordered_map>
#include <tuple>

namespace Valk::ExchangeGateway::TupleMap
{
    using boost::hash_value;
    using boost::hash_combine;
    template <typename T, typename U>
    auto hashTuple = [](const std::tuple<T, U>& singleTuple) -> size_t
    { 
        size_t seed{}; 
        hash_combine(seed, hash_value(std::get<0>(singleTuple)));
        hash_combine(seed, hash_value(std::get<1>(singleTuple)));
        return seed;
    };
    template <typename T, typename U>
    auto equalTuple = [](const std::tuple<T, U>& firstTuple, const std::tuple<T, U>& secondTuple) -> bool
    {
        return std::get<0>(firstTuple) == std::get<0>(secondTuple)
            && std::get<1>(firstTuple) == std::get<1>(secondTuple);
    };
    template <typename T, typename U, typename D>
    auto hashTripleTuple = [](const std::tuple<T, U, D>& singleTuple) -> size_t
    {
        size_t seed{};
        hash_combine(seed, hash_value(std::get<0>(singleTuple)));
        hash_combine(seed, hash_value(std::get<1>(singleTuple)));
        hash_combine(seed, hash_value(std::get<2>(singleTuple)));
        return seed;
    };
    template <typename T, typename U, typename D>
    auto equalTripleTuple = 
            [](const std::tuple<T, U, D>& firstTuple, const std::tuple<T, U, D>& secondTuple) -> bool
    {
        return std::get<0>(firstTuple) == std::get<0>(secondTuple)
            && std::get<1>(firstTuple) == std::get<1>(secondTuple)
            && std::get<2>(firstTuple) == std::get<2>(secondTuple);
    };

    using InstrumentFrequency = int;
    using TotalDelta = double;

    using FutureTupleUnorderedMap = std::unordered_map<std::tuple<TotalDelta, Instrument::InstrumentID, Platform::Price>,
        InstrumentFrequency, decltype(hashTripleTuple<TotalDelta, Instrument::InstrumentID, Platform::Price>),
        decltype(equalTripleTuple<TotalDelta, Instrument::InstrumentID, Platform::Price>)>;

    using OptionTupleUnorderedMap = std::unordered_map<std::tuple<Platform::Quantity, Instrument::InstrumentID>,
        InstrumentFrequency, decltype(hashTuple<Platform::Quantity, Instrument::InstrumentID>),
        decltype(equalTuple<Platform::Quantity, Instrument::InstrumentID>)>;
}

Все определения типов, которые вы видите, например Platform::Quantity и Platform::Price, являются определениями примитивных типов, например long long или int.

По какой-то причине я получаю следующие ошибки (снимок экрана проще, чем скопировать и вставить сюда), и я не уверен, почему. Здесь нет класса, конструктор копирования которого удален или не сгенерирован.

enter image description here

Спасибо за вашу помощь.

1 Ответ

1 голос
/ 25 мая 2020

Лямбды не могут быть построены по умолчанию. Итак, вам нужно передать операторы хеширования / равенства. В качестве альтернативы вы можете получить именованный тип лямбды и добавить конструктивность по умолчанию:

Простейшим было бы объединить ha sh соответственно операции равенства для двойных / тройных кортежей:

struct HashTuple {
    template <typename T, typename U>
    auto operator()(const std::tuple<T, U>& singleTuple) const -> size_t
    { 
        size_t seed{}; 
        hash_combine(seed, hash_value(std::get<0>(singleTuple)));
        hash_combine(seed, hash_value(std::get<1>(singleTuple)));
        return seed;
    }

    template <typename T, typename U, typename D>
    auto operator()(const std::tuple<T, U, D>& singleTuple) const -> size_t
    {
        size_t seed{};
        hash_combine(seed, hash_value(std::get<0>(singleTuple)));
        hash_combine(seed, hash_value(std::get<1>(singleTuple)));
        hash_combine(seed, hash_value(std::get<2>(singleTuple)));
        return seed;
    }
};

struct EqualTuple {
    template <typename... T>
    auto operator()(const std::tuple<T...>& firstTuple, const std::tuple<T...>& secondTuple) const -> bool {
        return firstTuple == secondTuple;
    }
};

Обратите внимание, что здесь используется эквивалентная std::tuple::operator== реализация

Теперь вы можете упростить типы:

std::unordered_map<std::tuple<double, int, double>, int, HashTuple, EqualTuple>
std::unordered_map<std::tuple<unsigned int, int>, int, HashTuple, EqualTuple>

Что я бы резюмировал с помощью простого помощника:

template <typename... Key> using FrequencyMap = 
    std::unordered_map<std::tuple<Key...>, InstrumentFrequency, HashTuple, EqualTuple>;

using FutureTupleUnorderedMap = FrequencyMap<TotalDelta, Instrument::InstrumentID, Platform::Price>;
using OptionTupleUnorderedMap = FrequencyMap<Platform::Quantity, Instrument::InstrumentID>;

Теперь у нас есть полностью рабочая демонстрация:

Live On Coliru

#include <boost/functional/hash.hpp>

#include <tuple>
#include <unordered_map>

struct Instrument {
    using InstrumentID = int;
};
struct Platform {
    using Quantity = unsigned;
    using Price = double;
};

namespace Valk::ExchangeGateway::TupleMap {
    struct HashTuple {
        template <typename T, typename U>
        auto operator()(const std::tuple<T, U>& singleTuple) const -> size_t
        { 
            using boost::hash_value;
            using boost::hash_combine;
            size_t seed{}; 
            hash_combine(seed, hash_value(std::get<0>(singleTuple)));
            hash_combine(seed, hash_value(std::get<1>(singleTuple)));
            return seed;
        }

        template <typename T, typename U, typename D>
        auto operator()(const std::tuple<T, U, D>& singleTuple) const -> size_t
        {
            using boost::hash_value;
            using boost::hash_combine;
            size_t seed{};
            hash_combine(seed, hash_value(std::get<0>(singleTuple)));
            hash_combine(seed, hash_value(std::get<1>(singleTuple)));
            hash_combine(seed, hash_value(std::get<2>(singleTuple)));
            return seed;
        }
    };

    struct EqualTuple {
        template <typename... T>
        auto operator()(const std::tuple<T...>& firstTuple, const std::tuple<T...>& secondTuple) const -> bool {
            return firstTuple == secondTuple;
        }
    };

    using InstrumentFrequency = int;
    using TotalDelta = double;


    template <typename... Key> using FrequencyMap = 
        std::unordered_map<std::tuple<Key...>, InstrumentFrequency, HashTuple, EqualTuple>;

    using FutureTupleUnorderedMap = FrequencyMap<TotalDelta, Instrument::InstrumentID, Platform::Price>;
    using OptionTupleUnorderedMap = FrequencyMap<Platform::Quantity, Instrument::InstrumentID>;
}

#include <boost/core/demangle.hpp>
#include <iostream>
int main() {
    {
        Valk::ExchangeGateway::TupleMap::FutureTupleUnorderedMap ftum;
        std::cout << boost::core::demangle(typeid(ftum).name()) << "\n";
    }
    {
        Valk::ExchangeGateway::TupleMap::OptionTupleUnorderedMap otum;
        std::cout << boost::core::demangle(typeid(otum).name()) << "\n";
    }
}

Печатает имена типов вы видели выше.

...