我读这文章今天上午从D.卡莱弗关于新的C++ 11功能"拖欠或删除功能",并且无法理解有关性能,即部分:
特殊成员函数的手动定义(即使它是微不足道的)通常比隐式定义的函数效率低.
通过谷歌搜索找到答案,我找到了同一作者的另一篇文章:
合成的构造函数和复制构造函数使实现能够创建比用户编写的代码更高效的代码,因为它可以应用其他方式并不总是可行的优化.
没有解释,但我不时阅读类似的说法.
但是如何写作:
class C { C() = default; };
Run Code Online (Sandbox Code Playgroud)
可以更有效率
class C { C(){} };
Run Code Online (Sandbox Code Playgroud)
?我虽然编译器足够聪明,可以检测到这种情况并对其进行优化.换句话说,编译器在看到=default而不是{}(void body function)时如何更容易优化?
编辑:编辑问题是为了添加"c ++ 11"标签,但这个问题仍然存在于c ++ 03上下文中:只是替换class C {C()=default;};为class C {};,所以不是真正的c ++ 11特定问题.
我在某处读到,如果捕获列表为空,则lambda函数应衰减为函数指针.我现在唯一能找到的参考是n3052.使用g ++(4.5和4.6)它可以按预期工作,除非lambda在模板代码中声明.
例如,以下代码编译:
void foo() {
void (*f)(void) = []{};
}
Run Code Online (Sandbox Code Playgroud)
但它在模板化时不再编译(如果foo在其他地方实际调用):
template<class T>
void foo() {
void (*f)(void) = []{};
}
Run Code Online (Sandbox Code Playgroud)
在上面的参考中,我没有看到这种行为的解释.这是g ++的临时限制,如果没有,是否有(技术)理由不允许这样做?
我想比较两个目录dir1和dir2不同文件系统的总大小,以便if diff -r dir1 dir2返回时0总大小将相等.该du命令返回磁盘使用情况,其选项--apparent-size无法解决问题.我现在使用类似的东西
find dir1 ! -type d |xargs wc -c |tail -1
Run Code Online (Sandbox Code Playgroud)
知道dir1大小的近似值.有更好的解决方案吗?
编辑:例如,我有(diff -r dir1 dir2返回0:它们相等):
du -s dir1 --> 540
du -s dir2 --> 166
du -sb dir1 --> 250815 (the -b option is equivalent to --apparent-size -B1)
du -sb dir2 --> 71495
find dir1 ! -type d |xargs wc -c --> 62399
find dir2 ! -type d |xargs wc -c --> …Run Code Online (Sandbox Code Playgroud) 我有一个类型列表,我想从中构建包含两个元素的所有组合的列表.例如:
namespace mpl = boost::mpl;
typedef mpl::vector<int, long> typelist;
// mpl magic...
// the wanted list is equivalent to:
typedef mpl::vector<pair<int, int>, pair<int, long>,
pair<long, int>, pair<long, long> > combinations;
Run Code Online (Sandbox Code Playgroud)
在这里,pair<T1,T2>可能是std::pair<T1,T2>,或mpl::vector<T1,T2>.这该怎么做?当我们考虑时,我也有兴趣删除重复项pair<T1, T2> == pair<T2, T1>.
谢谢.
在shell脚本中我希望变量p等于"$@",以便以下两行代码产生相同的结果:
for x in "$p"; do echo $x; done
for x in "$@"; do echo $x; done
Run Code Online (Sandbox Code Playgroud)
如果我这样做p=$@或者p="$@"这不起作用.当命令行参数的名称中有空格时,我找不到这个问题的简单解决方法(组合p="$@"和for x in $p(在$ p周围删除的引号)在没有空格时有效).
c++ ×3
bash ×2
c++11 ×2
boost-mpl ×1
command-line ×1
constructor ×1
lambda ×1
linux ×1
parameters ×1
performance ×1
shell ×1
templates ×1