给定以冒号分隔的路径列表,使用GNU Make获取以空格分隔的列表非常简单:
CPATHS := /usr/bin/foo:/usr/bin/baz:/usr/bin/baz
SPATHS := $(subst :, ,$(CPATHS))
Run Code Online (Sandbox Code Playgroud)
但是,我找不到朝着相反方向前进的好方法.以下hack确实有效(至少在安装了sed的情况下)但是我很确定只有使用Make的内部函数才能有更好的解决方法.
SPATHS := /usr/bin/foo /usr/bin/baz /usr/bin/baz
CPATHS := $(shell echo $(SPATHS) > tmp; sed 's/ \+/:/g' tmp; rm tmp)
Run Code Online (Sandbox Code Playgroud) 在他的"C++编程语言"(第4版)一书的"教程"一章5.3.5.3中,Bjarne Stroustrup撰写了关于该std::async功能的文章.
有一个明显的局限性:甚至不要考虑使用
async()共享需要锁定的资源的任务 -async()你甚至不知道thread将使用多少个,因为这async()可以根据它对可用系统资源的了解来决定在通话时.
类似的劝告可以在他的网站上的C++ 11-FAQ中找到.
"简单"是
async()/future设计最重要的方面; 期货也可以用在一般线程使用,但是甚至不认为使用async()来启动该做的我的任务/ O,操纵互斥体,或者以其他方式与其他任务进行交互.
有趣的是,当他在他的书的第42.4.6节中更详细地回到C++ 11的并发特性时,他没有详细说明这个限制.更有趣的是,在本章中他实际上还在继续(与他网站上的陈述相比):
一个简单而实际的用法
async()是生成一个任务来收集用户的输入.
asyncon 的文档cppreference.com根本没有提到任何这样的限制.
在阅读了导致最终形式的C++ 11标准的一些提议和讨论之后(遗憾的是我无法访问),我明白它async已经很晚才纳入C++ 11标准并且有很多讨论这个功能应该有多强大.特别是,我发现了文章中的Async Tasks in C++ 11:Not Quite There Yet Bartosz Milewski对实现时必须考虑的问题进行了非常好的总结async.
但是,所有讨论的问题都与thread_local如果线程被回收时未被破坏的变量,虚假死锁或数据访问违规有关,如果线程在操作中切换其任务并且两个任务分别持有mutex或recursive_mutex等等.这些都为特征的实现者严重关切,但如果我理解正确的话,目前规范async要求所有这些细节被执行或者在调用者线程或任务对用户隐藏的,如果作为一个新线程的任务创建.
所以我的问题是:我不允许做什么async,我被允许thread手动使用s,这个限制的原因是什么?
例如,以下程序有什么问题吗?
#include <future>
#include …Run Code Online (Sandbox Code Playgroud) concurrency multithreading asynchronous language-lawyer c++11
在C中有两种分配全局数组的方法:
静态
char data[65536];
Run Code Online (Sandbox Code Playgroud)动态
char *data;
…
data = (char*)malloc(65536); /* or whatever size */
Run Code Online (Sandbox Code Playgroud)问题是,哪种方法有更好的表现?多少钱?
理解它,第一种方法应该更快.
因为使用第二种方法,要访问数组,每次访问时都必须取消引用元素的地址,如下所示:
data包含指向数组开头的指针的变量使用第一种方法,编译器将data变量的地址硬编码到代码中,跳过第一步,因此我们有:
每次存储器访问相当于大约40个CPU时钟周期,因此,使用动态分配,特别是对于不频繁的读取,与静态分配相比可以显着降低性能,因为data可以通过一些更频繁访问的变量从缓存中清除变量.相反,解除引用静态分配的全局变量的成本是0,因为它的地址已经在代码中进行了硬编码.
它是否正确?
在我的项目中,我将使用过的点类型更改Eigen::Vector2f为Eigen::Vector2d并遇到了对齐问题.
这是代码的简化版本:
#include <vector>
#include <Eigen/Eigen>
int main()
{
std::vector<Eigen::Vector2d> points = { {0,0}, {0,1} };
}
Run Code Online (Sandbox Code Playgroud)
我收到以下运行时错误:
eigen3/Eigen/src/Core/DenseStorage.h:78: Eigen::internal::plain_array<double, 2, 0, 16>::plain_array() [T = double, Size = 2, MatrixOrArrayOptions = 0, Alignment = 16]: Assertion `(reinterpret_cast<size_t>(array) & 0xf) == 0 && "this assertion is explained here: " "http://eigen.tuxfamily.org/dox-devel/group__TopicUnalignedArrayAssert.html" " **** READ THIS WEB PAGE !!! ****"' failed.
Run Code Online (Sandbox Code Playgroud)
正如断言消息所示,我读到了固定大小的可矢量化特征对象所需的对齐.还有关于STL容器的小节.似乎我有两个选择:
Eigen::aligned_allocatorEIGEN_DEFINE_STL_VECTOR_SPECIALIZATION宏.两次尝试都不编译(使用GCC 4.8.3和Clang 3.5进行测试),因为编译器无法正确转换初始化列表.
这里改变了代码:
#include <vector> …Run Code Online (Sandbox Code Playgroud) 我在一个非常大的应用程序中发现了这个问题,从中做了一个SSCCE.我不知道代码是否有未定义的行为或-O2打破它.
用gcc a.c -o a.exe -O2 -Wall -Wextra -Werror它编译时打印5.
但是在编译时没有(例如)或取消注释2条注释行之一(防止内联)时打印25.-O2-O1
#include <stdio.h>
#include <stdlib.h>
// __attribute__((noinline))
int f(int* todos, int input) {
int* cur = todos-1; // fixes the ++ at the beginning of the loop
int result = input;
while(1) {
cur++;
int ch = *cur;
// printf("(%i)\n", ch);
switch(ch) {
case 0:;
goto end;
case 1:;
result = result*result;
break;
}
}
end:
return result;
}
int main() …Run Code Online (Sandbox Code Playgroud) 考虑一个(只读第三方)标头lib.h:
#define XYZ 42
Run Code Online (Sandbox Code Playgroud)
在源文件中,我想将该单词XYZ用于不相关的目的,并且不希望替换为42.但是,相同的源文件中,用于其他用途,我也确实想访问该值42从lib.h没有硬编码.如何将宏重命名XYZ为,比如说LIB_XYZ?
以下是不行的,因为预处理器要XYZ在时间LIB_XYZ替换发生,但XYZ一直没有定义:
#include "lib.h"
#define LIB_XYZ XYZ
#undef XYZ
Run Code Online (Sandbox Code Playgroud)
有没有办法欺骗预处理器LIB_XYZ在XYZ丢失之前扩展到它的最终值?
在关于inline函数声明中的关键字的许多争论中,有人会指出它在某些情况下实际上会使程序变慢 - 主要是由于代码爆炸,如果我是正确的.我自己从未在实践中遇到过这样的例子.什么是实际代码,使用inline可能会对性能有害?
我用gcc/clang构建了这段代码并得到了不同的结果:
#include <iostream>
#include <sstream>
int main() {
std::istream& is = 1 ? std::move(std::stringstream("")) : std::cin;
}
Run Code Online (Sandbox Code Playgroud)
std::stringstream(""))初始化左值引用?没错
prog.cc:5:63: error: call to implicitly-deleted copy constructor of 'istream' (aka 'basic_istream<char>')
std::istream& is = 1 ? std::move(std::stringstream("")) : std::cin;
^~~~~~~~
/usr/local/libcxx-3.4/include/c++/v1/istream:185:5: note: copy constructor is implicitly deleted because 'basic_istream<char, std::__1::char_traits<char> >' has a user-declared move constructor
basic_istream(basic_istream&& __rhs);
^
prog.cc:5:28: error: calling a protected constructor of class 'std::__1::basic_istream<char, std::__1::char_traits<char> >'
std::istream& is = 1 ? …Run Code Online (Sandbox Code Playgroud) 既然isnan可以是宏(在C++ 98中)或在命名空间std中定义的函数(在C++ 11中),这个简单的例子说明了编写在两种情况下都有效的代码的明显(并且可能是天真的)方式.
#include <cmath>
int main() {
double x = 0;
using namespace std;
isnan(x);
}
Run Code Online (Sandbox Code Playgroud)
但是,编译它会在GCC(使用-std = c ++ 11)和Clang中产生错误:
test.cc: In function ‘int main()’:
test.cc:6:10: error: call of overloaded ‘isnan(double&)’ is ambiguous
isnan(x);
^
test.cc:6:10: note: candidates are:
In file included from /usr/include/features.h:374:0,
from /usr/include/x86_64-linux-gnu/c++/4.8/bits/os_defines.h:39,
from /usr/include/x86_64-linux-gnu/c++/4.8/bits/c++config.h:426,
from /usr/include/c++/4.8/cmath:41,
from test.cc:1:
/usr/include/x86_64-linux-gnu/bits/mathcalls.h:234:1: note: int isnan(double)
__MATHDECL_1 (int,isnan,, (_Mdouble_ __value)) __attribute__ ((__const__));
^
In file included from test.cc:1:0:
/usr/include/c++/4.8/cmath:626:3: note: constexpr bool std::isnan(long double) …Run Code Online (Sandbox Code Playgroud) 说我想覆盖operator =所以我可以做类似的事情
Poly p1; // an object representing a polynomial
Poly p2; // another object of the same type
p2 = p1; // assigns all the contents of p1 to p2
Run Code Online (Sandbox Code Playgroud)
然后在我的实现中operator =,我有这样的事情:
Poly& Poly::operator=(const Poly &source) {
// Skipping implementation, it already works fine…
return *this;
}
Run Code Online (Sandbox Code Playgroud)
不介意实现,它已经正常工作.
我关心的是你什么时候会发生什么return *this?我知道它返回对象的引用,但这是怎么回事?
p2 = &p1
Run Code Online (Sandbox Code Playgroud) c ×4
c++ ×4
c++11 ×4
gcc ×3
performance ×2
arrays ×1
asynchronous ×1
clang ×1
cmath ×1
colon ×1
concurrency ×1
eigen3 ×1
gnu-make ×1
macros ×1
makefile ×1
optimization ×1
path ×1
space ×1