Tag: c ++ 11

std :: this_thread :: sleep_for()和GCC

当我尝试编译这个简单的程序时: #include<thread> void f() { std::this_thread::sleep_for(std::chrono::seconds(3)); } int main() { std::thread t(f); t.join(); } 在Ubuntu 10.04(32位)上使用gcc 4.4.3版本: $ g++ -std=c++0x -pthread a.cpp -oa 我得到: error: 'sleep_for' is not a member of 'std::this_thread' 我看着标题“线程”。 sleep_for()被_GLIBCXX_USE_NANOSLEEP保护 #ifdef _GLIBCXX_USE_NANOSLEEP … /// sleep_for template<typename _Rep, typename _Period> inline void sleep_for(const chrono::duration<_Rep, _Period>& __rtime) … 为什么_GLIBCXX_USE_NANOSLEEP没有定义? 我如何得到这个例子来编译? 更新2012年9月17日 (jogojapan):今天我遇到了同样的问题,使用GCC 4.7.1。 […]

将vector追加到vector的最佳方法

std::vector<int> a; std::vector<int> b; std::vector<int> c; 我想通过将b和c的元素附加到b来连接这三个向量。 这是最好的方法,为什么? 1)通过使用vector::insert : a.reserve(a.size() + b.size() + c.size()); a.insert(a.end(), b.begin(), b.end()); a.insert(a.end(), c.begin(), c.end()); b.clear(); c.clear(); 2)通过使用std::copy : a.reserve(a.size() + b.size() + c.size()); std::copy(b.begin(), b.end(), std::inserter(a, a.end())); std::copy(c.begin(), c.end(), std::inserter(a, a.end())); b.clear(); c.clear(); 3)通过使用std::move (从C++11 ): a.reserve(a.size() + b.size() + c.size()); std::move(b.begin(), b.end(), std::inserter(a, a.end())); std::move(c.begin(), c.end(), std::inserter(a, […]

删除decltype中的引用(返回T而不是T&,其中T&是decltype)

(如果您是C ++ 11专业版,请跳至粗体段落。) 比方说,我想写一个模板方法,它调用并返回types是模板参数的传递对象的结果: template<ReturnType, T> ReturnType doSomething(const T & foo) { return foo.bar(); // EDIT: Might also be an expression introducing a temp val } 所以T必须有一个方法ReturnType T::bar() const为了在这样的调用中使用: struct MyClass { … int bar() const; … }; … MyClass object; int x = doSomething<int, MyClass>(object); 我们不必写MyClass感谢types扣除和调用变成: int x = doSomething<int>(object); 但省略<int>也会导致编译错误,因为该方法不需要返回int以便事后分配给x (例如,它可以返回char )。 在C […]

C ++最令人烦恼的parsing

直接从http://herbsutter.com/2013/05/09/gotw-1-solution/取得 虽然widget w(); 对我来说很清楚,我不知道下面的代码怎么能成为一个函数声明? // same problem (gadget and doodad are types) // widget w( gadget(), doodad() ); // pitfall: not a variable declaration 这怎么可能?

boost :: lock_guard vs boost :: mutex :: scoped_lock

哪一个是首选boost::lock_guard或boost::mutex::scoped_lock ? 我正在使用Boost.Thread,希望在C ++ 11线程变为可用时转向它。 scoped_lock是下一个c ++标准的一部分吗? 有什么优势比其他优先? 注意 :我知道scoped_lock只是lock_guard的typedef 。 编辑:我错了scoped_lock 不是 lock_guard typedef 。 这是unique_lock的typedef 。

类似std :: transform的函数返回转换的容器

我想实现一个类似于std::transformalgorithm的函数,而不是通过我想创build的参数取得输出迭代器,并返回一个带有已转换input元素的容器。 假设它被命名为transform_container并且有两个参数:容器和函子。 它应该返回相同的容器types,但可能通过不同的元素types进行参数化(Functor可以返回不同types的元素)。 我想使用我的function,如下面的例子: std::vector<int> vi{ 1, 2, 3, 4, 5 }; auto vs = transform_container(vi, [] (int i) { return std::to_string(i); }); //vs will be std::vector<std::string> assert(vs == std::vector<std::string>({"1", "2", "3", "4", "5"})); std::set<int> si{ 5, 10, 15 }; auto sd = transform_container(si, [] (int i) { return i / 2.; }); //sd will […]

GCC错误与可变参数模板:“对不起,未实现:不能扩展'标识符…'到一个固定长度的参数列表”

在GCC的C ++ 11中进行可变模板编程时,偶尔会遇到一个错误,提示“对不起,未实现:无法将”标识符…“扩展为固定长度的列表。 如果我删除代码中的“…”,那么我会得到一个不同的错误:“错误:参数包不能用'…'扩展。 所以如果我有“…”,GCC会调用这个错误,如果我把“…”取出,GCC也会调用这个错误。 我已经能够处理这个问题的唯一方法是从头开始用一种不同的方法完全重写模板元程序,并且(幸运的是)我最终得到了不会导致错误的代码。 但我真的想知道我做错了什么。 尽pipe谷歌search,尽pipe进行了大量的实验,我不能确定我做了不同的模板代码之间产生这种错误,和没有错误的代码。 错误信息的措辞似乎意味着代码应该按照C ++ 11标准工作,但是GCC目前还不支持它。 或者,也许这是一个编译器错误? 这是一些产生错误的代码。 注意:我不需要你为我写一个正确的实现,而只是指出我的代码是什么导致了这个特定的错误 // Used as a container for a set of types. template <typename… Types> struct TypePack { // Given a TypePack<T1, T2, T3> and T=T4, returns TypePack<T1, T2, T3, T4> template <typename T> struct Add { typedef TypePack<Types…, T> type; }; }; […]

C ++ 11标准是否要求通过一个常量unordered_container访问元素的两次迭代以相同的顺序访问元素?

for (auto&& i : unordered_container) { /* … */ } for (auto&& i : unordered_container) { /* .. */ } 标准是否要求这两个循环访问相同顺序的元素(假定容器是未修改的)? 我对这个问题的分析 我读的标准和最好的我可以告诉答案是“不” 因为容器的迭代器是向前的,所以有一种语言要求a==b暗示++a==++b用于前向迭代器。 这意味着两个迭代将通过相同的path,如果他们都在同一个地方开始。 这将问题简化为标准是否需要container.begin() == container.begin() 。 我找不到任何需要的语言。

什么是声明和声明,它们的types是如何被标准解释的?

例如float (*(*(&e)[10])())[5]声明一个types为“指向10的指针的数组的指针”的variables返回指向数组的指针5 float “? 受@DanNissenbaum的讨论启发

是“枚举类”在C + +类types?

我使用cppreference阅读了C ++中的枚举声明。 然后我做了Enum类,并检查它是否是类types或不使用std::is_class 。 #include <iostream> enum class Enum { red = 1, blue, green }; int main() { std::cout << std::boolalpha; std::cout << std::is_class<Enum>::value << '\n'; } 然后我编译并运行在Linux平台上的G ++编译器,它打印出false值。 那么是不是类的types? 如果枚举是一个类的types,那么为什么我得到错误的价值?