Tag: c ++ 11

并发性:C ++ 11内存模型中的primefaces和易失性

一个全局variables在两个不同核心上的两个并发运行的线程之间共享。 线程写入和读取variables。 对于primefacesvariables,一个线程可以读取一个陈旧的值? 每个内核可能在其caching中具有共享variables的值,并且当一个线程写入其caching中的副本时,另一个内核中的另一个线程可能会从自己的caching中读取过时值。 或者编译器执行强大的内存sorting来从其他caching中读取最新值? c ++ 11标准库有std :: atomic支持。 这与volatile关键字有什么不同? 在上述情况下,volatile和atomictypes的performance会有什么不同?

在一个类中使用具有成员函数的通用std :: function对象

对于一个类,我想在一个存储std::function对象的map存储一些指向同一类的成员函数的函数指针。 但是这个代码我一开始就失败了: class Foo { public: void doSomething() {} void bindFunction() { // ERROR std::function<void(void)> f = &Foo::doSomething; } }; 我收到error C2064: term does not evaluate to a function taking 0 arguments在xxcallobj error C2064: term does not evaluate to a function taking 0 arguments结合一些奇怪的模板实例化错误。 目前我正在使用Visual Studio 2010/2011在Windows 8上工作,在VS 7上使用VS10工作也会失败。 错误必须基于一些奇怪的C ++规则,我不遵循。 编辑:我不使用提升。 这是MS编译器中集成的C ++ 11。

初始化程序列表和操作员的RHS

我不明白为什么初始化程序列表不能用在运营商的RHS上。 考虑: class foo { }; struct bar { template<typename… T> bar(T const&…) { } }; foo& operator<<(foo& f, bar const&) { return f; } int main() { foo baz; baz << {1, -2, "foo", 4, 5}; return 0; } 最新的Clang(海湾合作委员会)也抱怨说: clang.cc:14:9: error: initializer list cannot be used on the right hand side of operator […]

initializer_list和移动语义

我允许将元素移出std::initializer_list<T>吗? #include <initializer_list> #include <utility> template<typename T> void foo(std::initializer_list<T> list) { for (auto it = list.begin(); it != list.end(); ++it) { bar(std::move(*it)); // kosher? } } 由于std::intializer_list<T>需要特殊的编译器注意,并没有像C ++标准库的普通容器那样的价值语义,所以我宁愿安全,也不要抱歉。

为什么我可以使用私人types的汽车?

我有点惊讶,下面的代码编译和运行(vc2012&gcc4.7.2) class Foo { struct Bar { int i; }; public: Bar Baz() { return Bar(); } }; int main() { Foo f; // Foo::Bar b = f.Baz(); // error auto b = f.Baz(); // ok std::cout << bi; } 这段代码编译正确吗? 为什么它是正确的? 为什么我可以使用autotypes,而我不能使用它的名称(如预期的)?

为什么我不能push_back一个unique_ptr成vector?

这个程序有什么问题? #include <memory> #include <vector> int main() { std::vector<std::unique_ptr<int>> vec; int x(1); std::unique_ptr<int> ptr2x(&x); vec.push_back(ptr2x); //This tiny command has a vicious error. return 0; } 错误: $ g++ -std=gnu++0x main.cpp In file included from c:\mingw\bin\../lib/gcc/mingw32/4.5.0/include/c+ +/mingw32/bits/c++allocator.h:34:0, from c:\mingw\bin\../lib/gcc/mingw32/4.5.0/include/c+ +/bits/allocator.h:48, from c:\mingw\bin\../lib/gcc/mingw32/4.5.0/include/c+ +/memory:64, from main.cpp:6: c:\mingw\bin\../lib/gcc/mingw32/4.5.0/include/c++/bits/unique_ptr.h: I n member function 'void __gnu_cxx::new_allocator<_Tp>::construct(_Tp*, const _Tp&) [with _Tp […]

为什么列表初始化(使用花括号)比替代scheme更好?

MyClass a1 {a}; // clearer and less error-prone than the other three MyClass a2 = {a}; MyClass a3 = a; MyClass a4(a); 为什么? 我找不到答案,所以让我回答我自己的问题。

是传递const std :: string&作为参数的日子吗?

我听到了Herb Sutter最近的一次谈话,他build议通过const & std::vector和std::string的原因很大程度上消失了。 他build议现在写一个如下的函数是比较好的: std::string do_something ( std::string inval ) { std::string return_val; // … do stuff … return return_val; } 我知道return_val在函数返回的位置是一个右值,因此可以使用移动语义来返回,这很便宜。 然而, inval仍然大于参考的大小(通常作为指针实现)。 这是因为std::string具有各种组件,包括一个指向堆的指针和一个用于短string优化的成员char[] 。 所以在我看来,通过引用仍然是一个好主意。 任何人都可以解释为什么赫布可能会这样说?

我如何将C ++ 11支持添加到Code :: Blocks编译器?

我写了一些代码,需要有我的Code :: Blocks 12.11的C ++ 11支持。 我使用的是默认的GNU GCC编译器。 有什么办法可以做到这一点?

shared_ptr到一个数组:应该使用它?

只是一个关于shared_ptr的小查询。 使用shared_ptr指向数组是否是一个好习惯? 例如, shared_ptr<int> sp(new int[10]); 如果没有,那为什么不呢? 我已经知道的一个原因是不能递增/递减shared_ptr 。 因此,它不能像正常的数组指针一样使用。