简体   繁体   中英

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. I found that in file "type_trait", it uses following code to check whether an object is trivially copyable:

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

Question 1: where __is_trivially_copyable is implemented? I use grep to search the whole "include" directory and cannot find the definition of __is_trivially_copyable.

Question 2: how stl check the memory allocation between begin iterator and end iterator is continuous? 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.

A1: @HolyBlackCat is correct; __is_trivially_copyable is a compiler intrinsic. 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). If the iterators are pointers, then the memory is contiguous. (Other iterator types may support contiguous memory, but there's no way to detect that in general)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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