C++ friend class std::vector

后端 未结 1 1951
予麋鹿
予麋鹿 2021-01-20 14:56

Is it possible to do the following portably:

struct structure {
    structure() {}
private:
    // only allow container copy construct
    structure(const st         


        
相关标签:
1条回答
  • 2021-01-20 15:19

    No. vector (more precisely, the allocator passed into vector) can delegate the task of construction to a free function or another class, making the friendship useless.

    Even if you pass your own allocator, it may be rebound to a class internal to the implementation. Then the constructor for your class may be accessed from that class, not your allocator. So if that's your workaround, it's not guaranteed. (Although looking at GCC's implementation, it does scrupulously use the un-rebound allocator to construct such subobjects.)

    In GCC's libstdc++, no STL container template constructs the contained objects in the scope of a standard class or function.

    0 讨论(0)
提交回复
热议问题