1

Ok, so I a boost::bimap declared as so:

boost::bimap<object*, position> object_list;

Where object and position are a class and a struct, respectively.

Within the current object stored in the bimap, I want to find its own entry.

I am currently trying to do so like this:

void order::do_thing() {
    ...
    position pos = object_list.left.at(this);
    ...
}

I have trouble parsing the error output, but it seems like the at function of the bimap doesn't like the const-ness and/or reference-ness of this (and yes, I know this is an rvalue).

What would be the proper/suggested way to do this search?

Here is a snippet of the error:

order.cpp:117:56:   required from here
/usr/include/boost/bimap/container_adaptor/associative_container_adaptor.hpp:207:56: error: no match for call to ‘(const boost::bimaps::container_adaptor::detail::key_to_base_identity<object*, object* const>) (order* const&)’
                 this->template functor<key_to_base>()(k)

(Edit added to address sehe's comments and to point out the problem)

Apologies if my post was in bad etiquette, I am both new to posting on SO and I was under the assumption that just dumping all of my code (just to include the custom objects and templates for this section of code is hundreds of lines) would be considered bad form, so I trimmed it down to (what I thought was) the bare minimum to still get the issue.

And just to address the test-cases link, I do write test cases. In this case I was swapping out a vector for a bimap to add in the position code, and this actually fails to compile (though I will say I was not very explicit on it being a compilation error, and not a stack trace). I'm assuming you (sehe), thought I was talking about a runtime error, because otherwise I don't see how the link was relevant.

Anyway, I was trimming out code down to the actual minimum to reproduce the error so I could post it here, and then realized the actual issue. As shown above, the bimap holds values <object*, position> But the class trying to insert itself into the bimap is an order class: voidorder::do_thing() {

So it turned out to be a simple type error. Sorry, everyone, for asking a really dumb question. I guess that is what I get for working on projects at 3 AM.

4

1 回答 1

0

没有问题。

我能想到的唯一陷阱是你有this隐含的 const。在这种情况下,地图将不得不采用指向的指针 const object

在此处查看此示例。取消注释// const以查看 const 的变化:

Live On Coliru(非常量)

Live On Coliru(常量)

#include <boost/bimap.hpp>
#include <iostream>

using position = std::string;

#define USE_CONST // const

struct object {
    template <typename BiMap>
    position get_position(BiMap const& bimap) USE_CONST {
        return bimap.left.at(this);
    }
};

int main() {
    std::vector<object> instances(10);

    boost::bimap<object USE_CONST*, position> mapping;

    for (auto& instance : instances)
        mapping.insert({&instance, "instance #" + std::to_string(mapping.size()+1)});

    for (auto& instance : instances)
        std::cout << "Instance reports " << instance.get_position(mapping) << "\n";
}

印刷

Instance reports instance #1
Instance reports instance #2
Instance reports instance #3
Instance reports instance #4
Instance reports instance #5
Instance reports instance #6
Instance reports instance #7
Instance reports instance #8
Instance reports instance #9
Instance reports instance #10
于 2015-10-18T14:29:36.960 回答