C++错误C2280 - 试图引用一个已删除的函数 - 在基元类型上。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C++错误C2280 - 试图引用一个已删除的函数 - 在基元类型上。相关的知识,希望对你有一定的参考价值。

我正在实现我自己的两个无序地图,一个是接收一个有3个参数的key,另一个是有2个参数的tuple。以下是我的代码。

#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.

不知为什么,我得到了以下错误(截图比复制粘贴在这里容易),我不知道为什么。这里没有一个类的复制构造函数被删除或没有生成。

enter image description here

谢谢大家的帮助。

答案

lambdas不是默认可构造的。所以,你需要传递哈希质量运算符。或者,你可以推导出lambda的命名类型,并添加默认可构造性。

最简单的方法是结合哈希分别平等运算符来处理双三元组。

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

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

以上是关于C++错误C2280 - 试图引用一个已删除的函数 - 在基元类型上。的主要内容,如果未能解决你的问题,请参考以下文章

Visual Studio 2013 和 2015 中的 C++ 编译器错误 C2280“试图引用已删除的函数”

C++ 错误(C2280)试图访问已删除的函数 [关闭]

编译器错误 C2280,试图引用已删除的函数 operator=

试图找到已删除的函数

std::mutex 引起的 C2280 尝试引用已删除的函数

尝试引用已删除的函数时出错