uninitialized_fill_n不转发参数

时间:2018-08-30 23:19:59

标签: c++ c++11 initialization fill forward

我正在实现一个容器,按照import ldap.modlist as ml d = { 'a': 'word', 'cn': 'ou=example,ou=com' } e = { 'a': 'other word', 'cn': 'ou=example,ou=com'} ldif = ml.modifyModlist(d, e) print str(ldif) 的示例,它具有私有的PS1=> diff /Library/Python/2.7.12/site-packages/ldap/modlist.py /tmp/py2.7.15/build_modlist.py 4,10c4 < See http://www.python-ldap.org/ for details. < < $Id: modlist.py,v 1.18 2011/06/06 13:07:38 stroeder Exp $ < < Python compability note: < This module is known to work with Python 2.0+ but should work < with Python 1.5.2 as well. --- > See https://www.python-ldap.org/ for details. 15,31c9 < import string,ldap,ldap.cidict < < < def list_dict(l,case_insensitive=0): < """ < return a dictionary with all items of l being the keys of the dictionary < < If argument case_insensitive is non-zero ldap.cidict.cidict will be < used for case-insensitive string keys < """ < if case_insensitive: < d = ldap.cidict.cidict() < else: < d = {} < for i in l: < d[i]=None < return d --- > import ldap 36c14 < ignore_attr_types = list_dict(map(string.lower,(ignore_attr_types or []))) --- > ignore_attr_types = {v.lower() for v in ignore_attr_types or []} 38,39c16,17 < for attrtype in entry.keys(): < if ignore_attr_types.has_key(string.lower(attrtype)): --- > for attrtype, value in entry.items(): > if attrtype.lower() in ignore_attr_types: 43c21 < attrvaluelist = filter(lambda x:x!=None,entry[attrtype]) --- > attrvaluelist = [item for item in value if item is not None] 45c23 < modlist.append((attrtype,entry[attrtype])) --- > modlist.append((attrtype, value)) 71,72c49,50 < ignore_attr_types = list_dict(map(string.lower,(ignore_attr_types or []))) < case_ignore_attr_types = list_dict(map(string.lower,(case_ignore_attr_types or []))) --- > ignore_attr_types = {v.lower() for v in ignore_attr_types or []} > case_ignore_attr_types = {v.lower() for v in case_ignore_attr_types or []} 76,79c54,57 < attrtype_lower_map[string.lower(a)]=a < for attrtype in new_entry.keys(): < attrtype_lower = string.lower(attrtype) < if ignore_attr_types.has_key(attrtype_lower): --- > attrtype_lower_map[a.lower()]=a > for attrtype, value in new_entry.items(): > attrtype_lower = attrtype.lower() > if attrtype_lower in ignore_attr_types: 83,84c61,62 < new_value = filter(lambda x:x!=None,new_entry[attrtype]) < if attrtype_lower_map.has_key(attrtype_lower): --- > new_value = [item for item in value if item is not None] > if attrtype_lower in attrtype_lower_map: 86c64 < old_value = filter(lambda x:x!=None,old_value) --- > old_value = [item for item in old_value if item is not None] 97,110c75,81 < case_insensitive = case_ignore_attr_types.has_key(attrtype_lower) < old_value_dict=list_dict(old_value,case_insensitive) < new_value_dict=list_dict(new_value,case_insensitive) < delete_values = [] < for v in old_value: < if not new_value_dict.has_key(v): < replace_attr_value = 1 < break < add_values = [] < if not replace_attr_value: < for v in new_value: < if not old_value_dict.has_key(v): < replace_attr_value = 1 < break --- > if attrtype_lower in case_ignore_attr_types: > old_value_set = {v.lower() for v in old_value} > new_value_set = {v.lower() for v in new_value} > else: > old_value_set = set(old_value) > new_value_set = set(new_value) > replace_attr_value = new_value_set != old_value_set 120,121c91,92 < for a in attrtype_lower_map.keys(): < if ignore_attr_types.has_key(a): --- > for a, val in attrtype_lower_map.items(): > if a in ignore_attr_types: 124c95 < attrtype = attrtype_lower_map[a] --- > attrtype = val

std::vector

尽管如此,出于通用编程目的,最好使用算法而不是原始循环。事实证明,该标准具有unitialize_construct,它可以进行新的放置,实际上非常相似。

https://en.cppreference.com/w/cpp/memory/uninitialized_fill_n

但是,存在一个问题,template<class... Args> auto uninitialized_construct(Args&&... args){ auto cur = data_; for(size_type n = this->num_elements(); n > 0; --n, ++cur) alloc_traits::construct(allocator_, std::addressof(*cur), std::forward<Args>(args)...); }catch(...){destroy(cur); throw;} } 没有可变参数,因此我无法转发std::unitialized_fill_n

  1. 为什么会这样?是否有uninitialized_fill_n的另一个变体可以转发其构造参数?

  2. 如果没有,我应该打std::uninitialized_fill_n吗?

重申一下,我希望能够说出args...


编辑:据我从@Deduplicator的评论中了解到的,我在原始代码中存在错误,因为我多次从参数中移出参数。

unitialized_fill_n(data, size, value_type(std::forward<Args>(args)...))

一旦进行了更改,就可以用标准函数替换循环:

unitialized_fill_n(data, size, std::forward<Args>(args)...)

0 个答案:

没有答案