小编pro*_*ble的帖子

在C++中更改模板模板参数

我想设计一个类来创建内部类型,这些类型是作为模板参数传递的类型的变体.类似下面的非功能性示例:

template <typename T>
class BaseClass
{
public:
    typedef T InternalType;
    std::vector<InternalType> storage;
};

template <typename Base>
class Injector
{
public:
    typedef std::pair<typename Base::InternalType, typename Base::InternalType> RefinedType;
    Base<RefinedType> refinedStorage;
};

typedef Injector<BaseClass<int> > InjectedInt; // Should store vector<pair<int, int> >
Run Code Online (Sandbox Code Playgroud)

由于Base是完全指定的类型,Base<RefinedType> refinedStorage;将无法编译.简单地使用模板模板参数将不起作用,因为精炼类型需要基于嵌套模板的参数及其基本类型.

如何基于模板参数的完全指定类型和基本类型实现这种创建类型的模式?

编辑:我希望这是一个任意深度的复合,有多个注入器类型执行级联转换.因此,传递模板模板参数和基本参数变得相当笨拙(特别是在处理复合的基本情况时),理想的解决方案将使用更直接的语法.

c++ templates template-templates

10
推荐指数
3
解决办法
663
查看次数

在Python中存储对引用的引用?

使用Python,有没有办法存储对引用的引用,以便我可以在另一个上下文中更改引用引用的内容?例如,假设我有以下类:

class Foo:
   def __init__(self):
      self.standalone = 3
      self.lst = [4, 5, 6]
Run Code Online (Sandbox Code Playgroud)

我想创建类似于以下内容的东西:

class Reassigner:
   def __init__(self, target):
      self.target = target
   def reassign(self, value):
      # not sure what to do here, but reassigns the reference given by target to value
Run Code Online (Sandbox Code Playgroud)

如下代码

f = Foo()
rStandalone = Reassigner(f.standalone) # presumably this syntax might change
rIndex = Reassigner(f.lst[1])
rStandalone.reassign(7)
rIndex.reassign(9)
Run Code Online (Sandbox Code Playgroud)

会导致f.standalone等于7f.lst等于[4, 9, 6].

从本质上讲,这将是指向指针的类似物.

python reference

6
推荐指数
1
解决办法
167
查看次数

标签 统计

c++ ×1

python ×1

reference ×1

template-templates ×1

templates ×1