插入“这个”从构造函数转换为 STL 映射

发布于 2024-09-18 03:22:05 字数 1154 浏览 6 评论 0原文

版本 1

class Doh {
private:
    static std::map<const std::string, const Doh*> someMap;
    std::string stringValue_;
public:
    Doh(std::string str) : stringValue_(str) {
        Doh::someMap.insert(
            std::make_pair<const std::string,const Doh*>
                (this->stringValue_,this)
        );
    }
}

上面的内容在 MSVC 2010 中是可以的,但在 MSVC 2008 中却失败了——我猜这是因为对象插入到映射中时尚未构造(我遇到了内存访问冲突)。

因此,我尝试了延迟插入,它起作用了:

版本 2

Doh(std::string str) : stringValue_(str) {
    boost::thread(&Doh::insertIntoTheStaticMap,this);
}
void insertIntoTheStaticMap() {
    boost::this_thread::sleep(boost::posix_time::milliseconds(1000));
    Doh::someMap.insert(
        std::make_pair<const std::string,const Doh*>
            (this->stringValue_,this)
    );
}
But as you might be able to guess, my intention is to have the static Doh::someMap as a common lookup dictionary.

版本 1 不需要任何线程安全,因为我会在同一个线程中创建所有 Doh 实例 - 在初始化块中 - 这将被称为在进入 main() 之前通过动态初始化程序。

但对于 VERSION 2,简单的 sleep() 既不优雅也不可靠(更不用说,我可能需要在插入之前锁定映射)。

什么是好的 KISS 方法?

VERSION 1

class Doh {
private:
    static std::map<const std::string, const Doh*> someMap;
    std::string stringValue_;
public:
    Doh(std::string str) : stringValue_(str) {
        Doh::someMap.insert(
            std::make_pair<const std::string,const Doh*>
                (this->stringValue_,this)
        );
    }
}

The above was ok with MSVC 2010 but with MSVC 2008 it fails – and I guess it is because the object is not constructed yet when it is inserted in the map (I got a memory access violation).

So, I tried a delayed insertion, which worked:

VERSION 2

Doh(std::string str) : stringValue_(str) {
    boost::thread(&Doh::insertIntoTheStaticMap,this);
}
void insertIntoTheStaticMap() {
    boost::this_thread::sleep(boost::posix_time::milliseconds(1000));
    Doh::someMap.insert(
        std::make_pair<const std::string,const Doh*>
            (this->stringValue_,this)
    );
}

But as you might be able to guess, my intention is to have the static Doh::someMap as a common lookup dictionary.

VERSION 1 didn’t need any thread-safety because I would create all Doh instances in the same thread – in initialization blocks - which would be called by dynamic initializers before I enter main().

But with VERSION 2, the naïve sleep() is neither graceful nor reliable (not to mention, I might need to lock the map before insertion).

What would be a nice KISS approach?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

别闹i 2024-09-25 03:22:05

我看到的唯一潜在问题是静态成员的初始化(如果有多个源文件)。尝试用函数来保护它。

class Doh {
private:
    static std::map< std::string, Doh * > &get_map() {
        static std::map< std::string, Doh * > someMap;
        return someMap; // initialize upon first use
    }
    std::string stringValue_;
public:
    Doh(std::string str) : stringValue_(str) {
        get_map().insert(
            std::make_pair
                (this->stringValue_,this)
        );
    }
};

Only potential issue I see is the initialization of the static member, if there are multiple source files. Try guarding it with a function.

class Doh {
private:
    static std::map< std::string, Doh * > &get_map() {
        static std::map< std::string, Doh * > someMap;
        return someMap; // initialize upon first use
    }
    std::string stringValue_;
public:
    Doh(std::string str) : stringValue_(str) {
        get_map().insert(
            std::make_pair
                (this->stringValue_,this)
        );
    }
};
帝王念 2024-09-25 03:22:05

在这两个版本中都没有任何 stringvalue_ init 的迹象 - 当您在代码版本 1 中点击映射插入时,调试器会向您显示有关此键的什么信息?这个字段是如何设置的,它的类型是什么?

我想,在 VS2008 的调试器中运行它应该可以让您将故障点缩小到

源中。

In neither version is there any sign of init for stringvalue_ - what does the debugger show you about this key when you hit the map insert in version 1 of the code? How is this field set up, and what is its type?

Running this in the debugger for VS2008 should allow you to narrow down the point of failure into the <map> source, I would have thought.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文