简体   繁体   English

__is_trivially_copyable 如何在 g++ stl 中实现?

[英]how __is_trivially_copyable is implemented inside g++ stl?

In stl algorithm, when the value type is triviall copyable, the copy algorithm will use memmove to accelerate this operation.在 stl 算法中,当值类型是 triviall copyable 时,复制算法会使用 memmove 来加速这个操作。 I found that in file "type_trait", it uses following code to check whether an object is trivially copyable:我发现在文件“type_trait”中,它使用以下代码来检查 object 是否可以轻松复制:

template<typename _Tp>
struct is_trivially_copyable
: public integral_constant<bool, __is_trivially_copyable(_Tp)>
{ };

Question 1: where __is_trivially_copyable is implemented?问题一:__is_trivially_copyable 是在哪里实现的? I use grep to search the whole "include" directory and cannot find the definition of __is_trivially_copyable.我使用 grep 搜索整个“包含”目录,找不到 __is_trivially_copyable 的定义。

Question 2: how stl check the memory allocation between begin iterator and end iterator is continuous?问题2:stl如何检查开始迭代器和结束迭代器之间的memory分配是否连续? for example, if the iterator is belonged to dequeue, then we have to calling move operation for every element, rather than just one memmove like in vector or array.例如,如果迭代器属于出队,那么我们必须为每个元素调用移动操作,而不是像向量或数组中的一个 memmove。

A1: @HolyBlackCat is correct; A1:@HolyBlackCat 是正确的; __is_trivially_copyable is a compiler intrinsic. __is_trivially_copyable是编译器内在的。 There's no real way to tell (in the language) whether or not an arbitrary type is trivially copyable.没有真正的方法可以告诉(在语言中)任意类型是否可以轻松复制。

A2: The standard library looks to see if the iterators are raw pointers (as opposed to some class). A2:标准库查看迭代器是否是原始指针(与某些类相反)。 If the iterators are pointers, then the memory is contiguous.如果迭代器是指针,则 memory 是连续的。 (Other iterator types may support contiguous memory, but there's no way to detect that in general) (其他迭代器类型可能支持连续的 memory,但一般无法检测到)

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

相关问题 is_trivially_copyable 在我实现的构造函数和默认构造函数之间的行为不同 - is_trivially_copyable behaves differently between the constructor I implemented and the default std :: is_trivially_copyable错了吗? - Is std::is_trivially_copyable wrong? std :: is_trivially_copyable要求 - std::is_trivially_copyable requirements &#39;is_trivially_copyable&#39;不是&#39;std&#39;的成员 - ‘is_trivially_copyable’ is not a member of ‘std’ std :: is_trivially_copyable - 为什么volatile标量类型不能轻易复制? - std::is_trivially_copyable - Why are volatile scalar types not trivially copyable? C ++ is_trivially_copyable检查 - C++ is_trivially_copyable check is_trivially_copyable和is_trivially_copy_constructible有什么区别? - What is the difference between is_trivially_copyable and is_trivially_copy_constructible? std::is_trivially_copyable / std::is_trivially_copy_assignable 和数组包装类 - std::is_trivially_copyable / std::is_trivially_copy_assignable and array wrapping classes 为什么一个类是std :: is_trivially_copyable而不是std :: is_trivially_copy_constructible可以用于memcpy - Why has a class to be std::is_trivially_copyable and not std::is_trivially_copy_constructible to be useable with memcpy 在一个简单的可复制结构中,应该实现移动语义吗? - In a trivially copyable struct shall the move semantics be implemented?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM