简体   繁体   English

部分模板特化:std::allocator_traits?

[英]Partial template specialization for: std::allocator_traits?

Is it possible to specialize the std::allocator_traits, template like this?是否可以像这样专门化 std::allocator_traits, 模板?

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 ;
  
  } ;

}

The complete code compiles and runs in the std++20 allocator class and allocator, feature class (std::allocator_traits) specifications.完整的代码在 std++20 分配器 class 和分配器中编译和运行,具有 class (std::allocator_traits) 规范。 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?在那种情况下,我的问题实际上是关于实际正确性,是否根据对标准了解更多的人,是否禁止类似功能 class 重载,或者只是假设因为(完整)代码是可编译和可执行的,并且根据预期的计划 - 我可以认为我可以使用它吗?

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' ;
  }

Memory was allocated and freed correctly, I also checked with, Valgrind. Memory 已正确分配和释放,我还检查了 Valgrind。 Using Gdb, I checked and all functions were called from使用 Gdb,我检查并调用了所有函数

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.提前感谢您的任何见解、建议和建议。

It is permitted to specialize the std::allocator_traits template.允许专门化std::allocator_traits模板。 See [namespace.std]/2 :请参阅[namespace.std]/2

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.除非明确禁止,否则程序可以将任何标准库 class 模板的模板特化添加到命名空间std ,前提是 (a) 添加的声明取决于至少一种程序定义的类型,并且 (b) 特化满足标准库对原始模板。

It is not explicitly prohibited to specialize std::allocator_traits , therefore you may do so for the type FAllocator that you have defined.没有明确禁止专门化std::allocator_traits ,因此您可以为您定义的类型FAllocator这样做。 This rule applies to both full specializations and partial specializations.此规则适用于完全专业化和部分专业化。

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.该标准已经规定了每个成员类型的定义和每个成员 function 的行为,以至于如果您要编写自己的专业化,它基本上与原来的相同,这违背了这样做的目的。 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.我想您可以在不违反原始要求的情况下将日志语句添加到std::allocator_traits<...>::construct(...)之类的操作,但是将这些日志语句放入分配器本身会更容易。 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 .如果您坚持将它们放在您的allocator_traits专业化中,您还必须小心不要与[dcl.constexpr]/7 发生冲突。

I cannot see any situation where specializing std::allocator_traits makes sense.我看不到任何专门std::allocator_traits有意义的情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM