简体   繁体   English

C++ 错误 C2280 - 尝试引用已删除的 Function - 在原始类型上

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

I'm implementing my own two unordered maps, one that takes in a key that is a tuple with 3 arguments, and another that is a tuple with 2 arguments.我正在实现我自己的两个无序映射,一个包含一个键,它是一个具有 3 个 arguments 的元组,另一个是一个具有 2 个 arguments 的元组。 The following is my code:以下是我的代码:

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

All the typedefs you see, such as Platform::Quantity and Platform::Price are typedefs of primitive types, like long long or int .您看到的所有 typedef,例如Platform::QuantityPlatform::Price都是原始类型的 typedef,例如long longint

For some reason, I get the following errors (screenshot is easier than copy and paste here), and I'm not sure why.出于某种原因,我收到以下错误(屏幕截图比在此处复制和粘贴更容易),我不知道为什么。 There isn't a class here whose copy constructor is deleted or not generated.这里没有复制构造函数被删除或未生成的 class 。

在此处输入图像描述

Thanks for your help.谢谢你的帮助。

The lambdas aren't default constructable. lambdas 不是默认可构造的。 So, you need to pass the hash/equality operators.因此,您需要传递哈希/相等运算符。 Alternatively, you can derive a named type of the lambda and add default constructibility:或者,您可以派生 lambda 的命名类型并添加默认可构造性:

The simplest would be to combine the hash respectively equality ops for dual/triple tuples:最简单的方法是将 hash 分别组合为双/三元组的相等操作:

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::tuple::operator==实现

Now, you can simplify the types:现在,您可以简化类型:

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

Which I'd summarize with a simple helper:我会用一个简单的助手来总结一下:

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

Now we have a fully working demo:现在我们有一个完整的演示:

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

Prints the type names you saw above.打印您在上面看到的类型名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C ++错误C2280:尝试引用已删除的函数 - C++ Error C2280: Attempting to reference a deleted function C2280尝试引用已删除的功能 - C2280 attempting to reference a deleted function C2280 = 试图引用已删除的 function - C2280 = attempting to reference a deleted function 错误C2280:尝试引用已删除的功能 - error C2280: attempting to reference a deleted function Qt编译错误:C2280:尝试引用已删除的函数 - Qt compile error: C2280: attempting to reference a deleted function 错误C2280:尝试引用已删除的函数(原子 <int> ) - error C2280: attempting to reference a deleted function (atomic<int>) 编译器错误C2280,尝试引用已删除的函数operator = - Compiler error C2280, attempting to reference a deleted function operator= Visual Studio 2013 和 2015 中的 C++ 编译器错误 C2280“试图引用已删除的函数” - C++ Compiler Error C2280 "attempting to reference a deleted function" in Visual Studio 2013 and 2015 错误C2280:在声明C ++结构时尝试引用已删除的函数 - error C2280: attempting to reference a deleted function while declaring a C++ struct C++:带有 const int 的 class 的 vector.erase 实例给出“试图引用已删除的函数”错误 C2280 - C++: vector.erase instance of a class with const int gives "attempting to reference a deleted function" error C2280
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM