构建模板化容器类

时间:2017-01-30 15:19:56

标签: c++ c++11 gcc clang

作为我的自定义对象系统的一部分,我希望有一个Container类,它只包装第三方对象,以便它们可以集成到我的容器系统中。

假设我有以下第三方对象,

$removeReq = array_diff($messages, array("Required"));

容器类,

class Point { 
public:  
  int x;  
  Point(int y = 0) : x(y) {}  
  int value() { return x; } 
};

我正在尝试提供以下API,

template <typename T, typename... Args> class Container{  
   T dat; 
public:  
   Container(const Args &... args) { dat = T(args...); }  
   T data() { return dat; } 
};

我试图将传递给值构造函数的参数直接传递给类型T构造函数。

编辑:我收到编译错误 Container<Point> v = Container<Point>(1);

1 个答案:

答案 0 :(得分:2)

您需要在 from scrapy import Field, cmdline, Spider import os class FamilyItem(): name = Field() sons = Field() def __setitem__(self, item, value): self.__dict__[item] = value class SonsItem(): name = Field() grandsons = Field() def __setitem__(self, item, value): self.__dict__[item] = value def __iter__(self): return iter(self.grandsons) class GrandsonsItem(): name = Field() age = Field() weight = Field() sex = Field() def __setitem__(self, item, value): self.__dict__[item] = value class xSpider(Spider): name = 'xspider' start_urls = ['http://www.some_forum.de'] def parse(self, response): gs1 = GrandsonsItem() gs1['name'] = 'GS1' gs1['age'] = 18 gs1['weight'] = 50 gs2 = GrandsonsItem() gs2['name'] = 'GS2' gs2['age'] = 19 gs2['weight'] = 51 s1 = SonsItem() s1['name'] = 'S1' s1['grandsons'] = [dict(gs1), dict(gs2)] jenny = FamilyItem() jenny['name'] = 'Jenny' jenny['sons'] = [dict(s1)] yield {'item': jenny} def remove(filename): try: os.remove(filename) except OSError: pass def main(): output_file = "test.json" remove(output_file) cmdline.execute("scrapy runspider test.py -o test.json".split()) if __name__ == '__main__': main() 上创建构造函数,而不是类:

Args...