C++ 错误 C2280 - 尝试引用已删除的函数 - 关于原始类型

C++ Error C2280 - Attempting to Reference a Deleted Function - On Primitive Types

我正在实现我自己的两个无序映射,一个接受一个键,该键是一个具有 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::QuantityPlatform::Price都是基本类型的类型定义,例如long longint

出于某种原因,我收到以下错误(截屏比在此处复制和粘贴更容易),我不确定为什么。这里没有class拷贝构造函数被删除或没有生成

感谢您的帮助。

lambda 不是默认可构造的。因此,您需要传递 hash/equality 运算符。或者,您可以派生 lambda 的命名类型并添加默认可构造性:

最简单的方法是为 dual/triple 元组分别组合散列相等操作:

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;
    }
};

Note this uses the equivalent std::tuple::operator== implementation

现在,您可以简化类型:

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";
    }
}

打印您在上面看到的类型名称。