Tag: c ++ 11

弃用静态关键字…不再?

在C ++中,可以在翻译单元中使用static关键字来影响符号的可见性(variables或函数声明)。 在3092年,这是不赞成的: 附件D.2 [depr.static] 在命名空间范围内声明对象时,不推荐使用static关键字(见3.3.6)。 在n3225,这已被删除。 我能find的唯一文章是有些不正式的。 但是为了与C兼容(以及用C ++编译C程序的能力),它的确强调了这一点。 然而,直接编译一个C程序可能是一个令人沮丧的经验,所以我不确定是否值得考虑。 有谁知道它为什么被改变?

展平迭代器

是否有任何现有的迭代器实现(也许在boost)实现某种平坦化迭代器? 例如: unordered_set<vector<int> > s; s.insert(vector<int>()); s.insert({1,2,3,4,5}); s.insert({6,7,8}); s.insert({9,10,11,12}); flattening_iterator<unordered_set<vector<int> >::iterator> it( … ), end( … ); for(; it != end; ++it) { cout << *it << endl; } //would print the numbers 1 through 12

Visual Studio 2012中的C ++ 11function

Visual Studio 2012(VS2010之后的下一个版本)的预览版现在可用 。 有谁知道它支持什么新的C ++ 11function? (我目前无法尝试)。

unique_ptr有数组的用法吗?

std::unique_ptr支持数组,例如: std::unique_ptr<int[]> p(new int[10]); 但是它需要? 可能使用std::vector或std::array会更方便。 你觉得这个构造有什么用处吗?

我应该在C ++中使用std :: function或函数指针吗?

在C ++中实现一个callback函数时,我还应该使用C样式的函数指针: void (*callbackFunc)(int); 或者我应该使用std :: function: std::function< void(int) > callbackFunc;

为什么从`std :: async`返回的未来的析构函数被阻塞?

当试图回答另一个Stackoverflow问题时 ,我意识到这个简单的C ++ 11代码片段隐式地阻塞了调用线程: std::async(std::launch::async, run_async_task) 对我来说,这似乎是规范的C ++ 11方式asynchronous启动任务而不关心结果。 相反,人们必须明确地创build和分离一个线程(见提到的问题的答案 ),以实现这一点。 所以这里是我的问题:是否有任何std::future的析构函数被阻止的安全性/正确性的原因? 如果阻止get只是否是不够的,如果我不感兴趣的返回值或例外,它只是火灾和遗忘?

尾随返回types使用带有可变参数模板函数的decltype

我想写一个简单的加法器(笑),加起来每个参数,并返回与适当types的总和。 目前,我有这样的: #include <iostream> using namespace std; template <class T> T sum(const T& in) { return in; } template <class T, class… P> auto sum(const T& t, const P&… p) -> decltype(t + sum(p…)) { return t + sum(p…); } int main() { cout << sum(5, 10.0, 22.2) << endl; } 在GCC 4.5.1上,这对于2个参数来说似乎工作得很好,例如sum(2,5.5)返回7.5。 然而,比这更多的争论,我得到错误sum()是根本没有定义。 如果我这样宣布sum(): […]

是不是types的std :: function部分的模板参数(签名)?

鉴于以下代码,歧义背后的原因是什么? 我可以规避吗?还是我必须保持(烦人的)明确的演员? #include <functional> using namespace std; int a(const function<int ()>& f) { return f(); } int a(const function<int (int)>& f) { return f(0); } int x() { return 22; } int y(int) { return 44; } int main() { a(x); // Call is ambiguous. a(y); // Call is ambiguous. a((function<int ()>)x); // Works. a((function<int […]

如何在无序容器中为用户定义的types专门化std :: hash <Key> :: operator()?

为了支持std::unordered_set<Key>和std::unordered_map<Key, Value>用户定义的键types,必须提供operator==(Key, Key)和一个哈希仿函数: struct X { int id; /* … */ }; bool operator==(X a, X b) { return a.id == b.id; } struct MyHash { size_t operator()(const X& x) const { return std::hash<int>()(x.id); } }; std::unordered_set<X, MyHash> s; 用std::unordered_set<X>写一个types为X的默认散列会更方便,就像编译器和库一样。 经过咨询 C ++标准草案N3242§20.8.12 [unord.hash]和§17.6.3.4[hash.requirements], Boost.Unordered g ++ include\c++\4.7.0\bits\functional_hash.h VC10 include\xfunctional 堆栈溢出中的各种相关问题 似乎有可能专门化std::hash<X>::operator() : namespace std […]

在Eclipse CDT(Juno / Kepler / Luna)索引器中启用C ++ 11

如何在Juno / Kepler / Luna的Eclipse CDT索引器上启用对新C ++标准的支持?