如何减轻面向用户的API在模板类中共享成员的影响? [英] How to mitigate user-facing API Effect of shared members in templated classes?

查看:116
本文介绍了如何减轻面向用户的API在模板类中共享成员的影响?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个类型的查找表,我可以为给定的整数构建:

Let's say I have a type of lookup table which I can build for a given integer:

class FooLookupTable {
    ...
public:
    FooLookupTable(int radix) { 
        ...
    }
};

然后有一个类的模板参数是同一个整数,并且其构造函数初始化一个成员实例查找表:

Then there's a class whose template parameter is that same integer, and whose constructor initializes a member instance of this lookup table:

template <int radix> class Foo {
    ...
private:
    FooLookupTable table;
public:
    Foo () : FooLookupTable (radix) {
        ...
    }
};

在我的代码中,我使用不同的radix值来实例化:

Throughout my code I instantiate these with various values of radix:

 int main() {
     ...
     Foo<1> myFoo;
     Foo<1> yourFoo;
     Foo<10> theirFoo;
     ...
 }

线程或API问题。但它不是在 myFoo yourFoo 之间共享1的基数表。我可以硬编码对一个假定的线程库的依赖,并构建一个满足需求的全局映射。但我的问题是:

This works and doesn't create any hairy threading or API issues. But it's not sharing the radix table for 1 between myFoo and yourFoo. I could hardcode a dependency on an assumed thread library, and build a global map that's filled on-demand. But my question is:

在现代的C ++ 11世界中,有一个干净的方式设计Foo的库,标准库?

我想为此使用静态成员,因为模板类的每个单独实例化只创建一个静态成员变量。但是这带来了谁负责为静态成员声明空间的任何人必须知道正确的初始化它的方式的问题:

I thought of using a static member for this, since each separate instantiation of a template class creates only one static member variable. But this brings up the question of who is responsible for declaring the space for the static member and whoever does so has to "know the right way to initialize it":

 FooLookupTable Foo<1>::table (1);
 FooLookupTable Foo<10>::table (10);

 int main() {
     ...
     Foo<1> myFoo;
     Foo<1> yourFoo;
     Foo<10> theirFoo;
     ...
 }

阅读主题上写的内容, a href =http://stackoverflow.com/questions/1819131/c-static-member-initalization-template-fun-inside> C ++静态成员初始化(模板fun里面)似乎不打开很多希望...除非我错过了一些东西。此外,如果 Foo 实例本身是静态的,会发生什么? : - /

Reading what's written on the subject like " C++ Static member initalization (template fun inside) " doesn't seem to turn up much hope...unless I'm missing something. Also, what would happen if Foo instances themselves were static? :-/

推荐答案

如果您希望确保所有实例化的Foo <1, >共享同一个表。这是因为,跨越模块边界,如果模块A创建Foo <1>并且模块B创建Foo <1>,则它将复制静态成员。

static members of templates won't work well across APIs if you want to ensure all instantiations of Foo<1> share the same table. This is because, going across module boundaries, if module A creates Foo<1> and module B creates Foo<1>, it will duplicate the static members.

你最好的办法是在运行时将这些整数映射到相应的表。

I think your best bet is to map those integers to their corresponding tables at runtime.


我可以硬编码一个假设线程库的依赖,
a根据需要填充的全球地图。但我的问题是:

I could hardcode a dependency on an assumed thread library, and build a global map that's filled on-demand. But my question is:

在现代的C ++ 11世界中,有一个干净的方式设计一个库
Foo没有依赖关系标准的
库?

In the modern C++11 world, is there a clean way designing a library for Foo which does not have dependencies outside of the standard libraries?

作为一个实际的答案,没有,还没有。如果你想要线程安全(除非你可以设置一个共享容器是线程安全和无锁,没有第三方依赖),你需要关键部分/互斥表你的表和C ++没有什么开箱即用

As a practical answer, no, not yet. You need critical sections/mutual exclusion for your table if you want thread safety (unless you can manage to make a shared container that is thread safe and lock-free with no third party dependencies) and there's nothing out of the box in C++11 that's implemented yet (at least in popular compilers) to give you cross-platform concurrency AFAIK.

在未来,C ++ 11的并发功能将支持互斥体和原子性(至少在流行的编译器中)开箱即用,但流行的编译器尚未实现这一功能,但据我所知。

In the future, C++11's concurrency features will support mutexes and atomics out of the box, but popular compilers haven't implemented this feature yet to my knowledge.

这篇关于如何减轻面向用户的API在模板类中共享成员的影响?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆