Checking if a sequence container is contiguous in memory

后端 未结 3 490
臣服心动
臣服心动 2021-01-07 23:19

Is there a way to check if a sequence container is contiguous in memory? Something like:

#include 
#include 
#include 

        
3条回答
  •  说谎
    说谎 (楼主)
    2021-01-08 00:09

    No. The C++ Standard guarantees there are no false negatives. (i.e., std::vector, std::string, std::array, and basic arrays are promised to be stored contiguously).

    However, the C++ Standard doesn't guarantee there are no false positives.

    int main() {
       std::unique_ptr n1(new Node);
       std::unique_ptr n2(new Node);
       n1->next = n2; // n1 and n2 might be contiguous, but might not be
    }
    

    Thus, your type trait could be wrong some of the time. If it's wrong some of the time, it's not a type trait; rather, it's an instance trait.

提交回复
热议问题