部分模板特化:std::allocator_traits? [英] Partial template specialization for: std::allocator_traits?

查看:31
本文介绍了部分模板特化:std::allocator_traits?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以像这样专门化 std::allocator_traits 模板?

Is it possible to specialize the std::allocator_traits, template like this?

namespace falloc_ {

  template<class Tp> class FAllocator ;

}

// partial spec for all falloc_::FAllocator<U>, std::allocator_traits

template<typename Tp> 
struct std::allocator_traits<falloc_::FAllocator<Tp> > {

 using allocator_type = falloc_::FAllocator<Tp> ;
 using value_type = typename allocator_type::value_type ;

 // ...
 // All the components I need here, I will include all the code if You need them.
 // ...

} ;


namespace falloc_ {

  template<class Tp> class FAllocator {

    public:
       using value_type = Tp ;
  
  } ;

}

完整代码编译运行在std++20分配器类和分配器、特征类(std::allocator_traits)规范中.在那种情况下,我的问题实际上是关于实际的正确性,无论是根据对标准有更多了解的人,是否禁止类似的要素类重载,或者只是假设(完整的)代码是可编译和可执行的,并且根据预期计划 - 我可以考虑使用它吗?

The complete code compiles and runs in the std++20 allocator class and allocator, feature class (std::allocator_traits) specifications. In that case, my question is actually about practical correctness, whether according to people who know more about the standard, whether similar feature class overloading is forbidden, or simply assume that since the (complete) code is compileable and executable and works according to the intended plan - can I consider that I can use it?

另外,我用代码测试了分配器:

Additionally, I tested the allocator with the code:

  std::vector<int, falloc_::FAllocator<int> > mv ;

  for (size_t i = 0 ; i < 5 ; i++) {
    mv.push_back(i) ;
  }

  for (int i = 4 ; i > -1 ; i--) {
    std::cout << "mv[" << i << "]: " << mv[i] << '\n' ;
  }

内存已正确分配和释放,我还检查了 Valgrind.使用 Gdb,我检查并从

Memory was allocated and freed correctly, I also checked with, Valgrind. Using Gdb, I checked and all functions were called from

std::allocator_traits<falloc_::FAllocator<U> > // U aka int for std::vector<int, falloc_::FAllocator<int> > 

预先感谢您提供任何见解、建议和意见.

Thank You in advance for any insights, suggestions and advice.

推荐答案

允许专门化 std::allocator_traits 模板.请参阅[namespace.std]/2:

It is permitted to specialize the std::allocator_traits template. See [namespace.std]/2:

除非明确禁止,否则程序可以向名称空间 std 添加任何标准库类模板的模板特化,前提是 (a) 添加的声明依赖于至少一个程序定义的类型和 (b) 专业化满足原始模板的标准库要求.

Unless explicitly prohibited, a program may add a template specialization for any standard library class template to namespace std provided that (a) the added declaration depends on at least one program-defined type and (b) the specialization meets the standard library requirements for the original template.

没有明确禁止专门化 std::allocator_traits,因此您可以为您定义的类型 FAllocator 这样做.此规则适用于完全专业化和部分专业化.

It is not explicitly prohibited to specialize std::allocator_traits, therefore you may do so for the type FAllocator that you have defined. This rule applies to both full specializations and partial specializations.

请记住,您的专业确实必须满足原始模板的所有要求.标准已经规定了每个成员类型的定义和每个成员函数的行为,以至于如果您要编写自己的专业化,它会与原始的基本相同,这违背了这样做的目的.我想您可以在不违反原始要求的情况下执行诸如将日志记录语句添加到 std::allocator_traits<...>::construct(...) 之类的操作,但是将这些日志记录放在分配器本身中的语句.如果你坚持把它们放在你的 allocator_traits 专业化中,你还必须小心不要与 [dcl.constexpr]/7.

Do bear in mind that your specialization must indeed meet all requirements of the original template. The standard already specifies the definition of each member type and the behaviour of each member function to such an extent that if you were to write your own specialization, it would basically be identical to the original, which defeats the purpose of doing so. I suppose you could do something like add logging statements to std::allocator_traits<...>::construct(...) without violating the original requirements, but it's easier to just put those logging statements in the allocator itself. If you insist on putting them in your allocator_traits specialization, you also have to be careful to not run afoul of [dcl.constexpr]/7.

我看不出在任何情况下专门化 std::allocator_traits 是有意义的.

I cannot see any situation where specializing std::allocator_traits makes sense.

这篇关于部分模板特化:std::allocator_traits?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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