在下面的程序中,我尝试print使用函数本地互斥对象使函数成为线程安全的:
#include <iostream>
#include <chrono>
#include <mutex>
#include <string>
#include <thread>
void print(const std::string & s)
{
// Thread safe?
static std::mutex mtx;
std::unique_lock<std::mutex> lock(mtx);
std::cout <<s << std::endl;
}
int main()
{
std::thread([&](){ for (int i = 0; i < 10; ++i) print("a" + std::to_string(i)); }).detach();
std::thread([&](){ for (int i = 0; i < 10; ++i) print("b" + std::to_string(i)); }).detach();
std::thread([&](){ for (int i = 0; i < 10; ++i) print("c" + std::to_string(i)); }).detach();
std::thread([&](){ for (int …Run Code Online (Sandbox Code Playgroud) 我想使用创建一个包含正则表达式作为键值的对象.我尝试使用以下语法:
var kv = {
/key/g : "value"
};
Run Code Online (Sandbox Code Playgroud)
但它根据JavaScript lint失败:
SyntaxError: invalid property id
Run Code Online (Sandbox Code Playgroud)
我该如何解决?
背景:我之所以这样做,是为了实现修复HTTP API结果中错误的unicode的解决方法.我知道这是非常hackish,但由于我无法控制API服务器代码,我认为这是我能做的最好的.
目前我通过键数组和值数组实现了映射:
function fixUnicode(text) {
var result = text;
var keys = [];
var values = [];
keys.push(/é/g); values.push("é");
keys.push(/è/g); values.push("è");
keys.push(/ê/g); values.push("ê");
keys.push(/ë/g); values.push("ë");
keys.push(/Ã /g); values.push("à");
keys.push(/ä/g); values.push("ä");
keys.push(/â/g); values.push("â");
keys.push(/ù/g); values.push("ù");
keys.push(/û/g); values.push("û");
keys.push(/ü/g); values.push("ü");
keys.push(/ô/g); values.push("ô");
keys.push(/ö/g); values.push("ö");
keys.push(/î/g); values.push("î");
keys.push(/ï/g); values.push("ï");
keys.push(/ç/g); values.push("ç");
for (var i = 0; i < keys.length; ++i) {
result = result.replace(keys[i], …Run Code Online (Sandbox Code Playgroud) 在矢量上调用concat会返回一个列表.作为一个总的菜鸟我会期望结果也是一个矢量.为什么要转换成列表?
例:
user=> (concat [1 2] [3 4] [5 6])
(1 2 3 4 5 6)
; Why not: [1 2 3 4 5 6] ?
Run Code Online (Sandbox Code Playgroud) 注意:此问题已重命名并缩小,以使其更具针对性和可读性.大多数评论都涉及旧文本.
根据标准,不同类型的对象可能不共享相同的存储位置.所以这不合法:
std::array<short, 4> shorts;
int* i = reinterpret_cast<int*>(shorts.data()); // Not OK
Run Code Online (Sandbox Code Playgroud)
但是,该标准允许此规则的例外:可以通过指向char或的指针访问任何对象unsigned char:
int i = 0;
char * c = reinterpret_cast<char*>(&i); // OK
Run Code Online (Sandbox Code Playgroud)
但是,我不清楚这是否也允许反过来.例如:
char * c = read_socket(...);
unsigned * u = reinterpret_cast<unsigned*>(c); // huh?
Run Code Online (Sandbox Code Playgroud) 注意:我知道boost::variant,但我对设计原则感到好奇.这个问题主要是为了自我教育.
在我目前的工作中,我找到了一个旧的变体类实现.它是用a实现的union,只能支持少数几种数据类型.我一直在考虑如何设计改进版本.经过一些修修补补后,我得到了似乎有用的东西.但是我想知道你对它的看法.这里是:
#include <iostream>
#include <map>
#include <stdexcept>
#include <string>
#include <typeinfo>
#include <boost/shared_ptr.hpp>
class Variant
{
public:
Variant() { }
template<class T>
Variant(T inValue) :
mImpl(new VariantImpl<T>(inValue)),
mClassName(typeid(T).name())
{
}
template<class T>
T getValue() const
{
if (typeid(T).name() != mClassName)
{
throw std::logic_error("Non-matching types!");
}
return dynamic_cast<VariantImpl<T>*>(mImpl.get())->getValue();
}
template<class T>
void setValue(T inValue)
{
mImpl.reset(new VariantImpl<T>(inValue));
mClassName = typeid(T).name();
}
private:
struct AbstractVariantImpl
{
virtual ~AbstractVariantImpl() {}
};
template<class T>
struct VariantImpl : …Run Code Online (Sandbox Code Playgroud) 我安装了Xcode 4.3并想测试这个C++ 11程序:
#include <type_traits>
int main()
{
}
Run Code Online (Sandbox Code Playgroud)
但是,它找不到type_traits标题:
~ $ c++ -o test main.cpp
main.cpp:1:10: fatal error: 'type_traits' file not found
#include <type_traits>
^
1 error generated.
Run Code Online (Sandbox Code Playgroud)
看来我正在使用正确的编译器:
~ $ c++ -v
Apple clang version 3.1 (tags/Apple/clang-318.0.45) (based on LLVM 3.1svn)
Target: x86_64-apple-darwin11.3.0
Thread model: posix
Run Code Online (Sandbox Code Playgroud)
我检查了默认的包含路径:
~ $ `c++ --print-prog-name=cc1plus` -v
ignoring nonexistent directory "/usr/include/c++/4.2.1/i686-apple-darwin11"
ignoring nonexistent directory "/Applications/Xcode.app/Contents/Developer/usr/llvm-gcc-4.2/lib/gcc/i686-apple-darwin11/4.2.1/../../../../i686-apple-darwin11/include"
#include "..." search starts here:
#include <...> search starts here:
/usr/include/c++/4.2.1
/usr/include/c++/4.2.1/backward
/usr/local/include
/Applications/Xcode.app/Contents/Developer/usr/llvm-gcc-4.2/lib/gcc/i686-apple-darwin11/4.2.1/include
/usr/include
/System/Library/Frameworks …Run Code Online (Sandbox Code Playgroud) 如何将参数包拆分为两个相等的部分?
例如,我想做这样的事情:
template<typename T> T sum(const T& t)
{ return t; }
template<typename T> T sum(const T& t1, const T& t2)
{ return t1 + t2; }
template<typename ...T> T sum(T&& ...t)
{ sum(first_half(t)...) + sum(second_half(t)...); }
Run Code Online (Sandbox Code Playgroud) 在本地删除文件后跟svn update会恢复该文件的本地副本.但是,这似乎不适用于文件夹.有没有办法使它适用于文件夹?
编辑:这是控制台输出的样子:
C:\svn\Google Project Hosting\xulwin\xulrunnersamples>rmdir /S /Q treeview
C:\svn\Google Project Hosting\xulwin\xulrunnersamples>svn up
D treeview
Updated to revision 50.
Run Code Online (Sandbox Code Playgroud)
编辑2:我已经使用svn超过两年了,我之前从未遇到过这个错误.但我没有改变任何设置.我唯一能想到的是,我今天在外部硬盘驱动器上创建了一个带有Tortoise SVN的本地存储库来备份一些不相关的旧东西.但这不应该是问题的原因,因为行为发生在本地存储库的本地副本和Google Project Hosting的代码中.
EDIT3:WTF我突然无法重现这个bug.这一切都可以正常运作.但我没有改变任何事情.
EDIT4:在EDIT1中,文件似乎被标记为已删除,但它不是因为svn commit命令没有做任何事情.Tortoise检查修改是否也没有列出任何内容.
注意:当问题仍然存在时,完全递归更新确实还原了该文件夹.
我正在使用Visual Studio 2005创建一个开源C++库.我想提供预构建的库以及源代码.这些用VS2005构建的库是否也适用于较新版本的Visual Studio(特别是VS Express Edition 2008)?或者我是否需要为每个VS版本提供单独的库?
c++ ×6
c++11 ×3
clang ×1
clojure ×1
emacs ×1
javascript ×1
svn ×1
templates ×1
type-punning ×1
xcode ×1