std :: copy / memcpy / memmove优化 [英] std::copy/memcpy/memmove optimizations
问题描述
我查看了GCC STL(4.6.1),并看到 std :: copy()
使用优化版本的情况下内置 __is_trivial()
计算为 true
。
I looked into the GCC STL (4.6.1) and saw that std::copy()
uses an optimized version in case the builtin __is_trivial()
evaluates to true
.
std :: copy()和 std :: reverse_copy()
模板对于复制数组中的元素非常有用,我想使用它们。但是,我有一些类型(它们是模板实例化的结果),它们是包含一些琐碎值,没有指针,没有复制构造函数或赋值运算符的结构体。
Since the std::copy()
and std::reverse_copy()
templates are very useful for copying elements in arrays, I would like to use them. However, I have some types (which are results of template instantiations) that are structs that contain some trivial values, no pointers and have no copy constructor or assignment operator.
G ++聪明到足以明白我的类型其实是微不足道的?在C ++ 98中有什么方法来确保STL实现知道我的类型是微不足道的?
Is G++ smart enough to figure out that my type in fact is trivial? Is there any way in C++98 to make sure an STL implementation knows that my type is trivial?
我想在C ++ 11中,事情会变得方便使用 is_trivial<>
类型特征。是这样吗?
I guess that in C++11, things will become convenient using the is_trivial<>
type trait. Is this right?
谢谢!
编辑:对不起这么晚,一个非常简单的类型
类的示例,它对GCC和llvm不重要。任何想法?
Sorry for being so late with this, but here is an example of a pretty simple Type
class that is not trivial to GCC and llvm. Any ideas?
#include <iostream>
struct Spec;
template <typename TValue, typename TSpec>
class Type
{
public:
TValue value;
Type() : value(0) {}
};
int main()
{
std::cerr << "__is_trivial(...) == "
<< __is_trivial(Type<char, Spec>) << '\n';
return 0;
}
推荐答案
意味着
。
There has been some debate over what trivial
meant.
例如,你的例子不可简单地构造, c $ c> std :: is_trivially_default_constructible 会返回false我想)。
For example, your example is not trivially constructible as far as I can tell (std::is_trivially_default_constructible
would return false I think).
在你的case,我想你会需要新的trait < a href =http://en.cppreference.com/w/cpp/types/is_trivially_copyable =nofollow> std :: is_trivially_copyable
,这是更细粒度。所以...升级您的编译器?
In your case, I think you would need the new trait std::is_trivially_copyable
, which is more fine grained. So... upgrade your compiler ?
这篇关于std :: copy / memcpy / memmove优化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!