Tag: c ++ 11

std ::绑定一个绑定函数

我在检测为什么这是不编译的麻烦。 我有一些lambda函数返回基于一些参数的std::function 。 我已经缩小了我的问题到这个片段(这不使用lambdas,但完美地再现了我的错误): #include <functional> #include <iostream> struct foo { template<class T> void bar(T data) { std::cout << data << "\n"; } }; void some_fun(const std::function<void(int)> &f) { f(12); } int main() { foo x; auto f = std::bind(&foo::bar<int>, x, std::placeholders::_1); auto w = std::bind(some_fun, f); w(); } 对w()的调用产生了那些可爱的海湾合作委员会错误输出之一,我不知道什么是错的。 这是gcc 4.6.1所回应的错误: g++ -std=c++0x test.cpp […]

为什么auto x {3}会推导一个initializer_list?

我喜欢在C + + 11 auto 。 太棒了 但是它有一个矛盾让我很紧张,因为我一直在绊倒它: int i = 3; // i is an int with value 3 int i = int{3}; // i is an int with value 3 int i(3); // i is an int with value 3 (possibly narrowing, not in this case) int i{3}; // i is an int […]

C ++线程,std :: system_error – 操作不允许?

所以我写了一个程序来testing64位kubuntu linux版本13.04上的线程。 其实我是从编写testing程序的其他人那里抢走了代码。 #include <cstdlib> #include <iostream> #include <thread> void task1(const std::string msg) { std::cout << "task1 says: " << msg << std::endl; } int main(int argc, char **argv) { std::thread t1(task1, "Hello"); t1.join(); return EXIT_SUCCESS; } 我编译使用: g++ -pthread -std=c++11 -c main.cpp g++ main.o -o main.out 然后跑: ./main.out 另外,当我'ls -l'时,main.out像所有可执行文件一样以绿色文本显示,但在名称末尾有一个星号。 为什么是这样? 回到手头的问题:当我运行main.out时,出现一个错误,它说: terminate called […]

为每个可变参数和一个数组调用一个函数

所以我有一些typesX : typedef … X; 和一个模板函数f : class <typename T> void f(X& x_out, const T& arg_in); 然后一个函数g : void g(const X* x_array, size_t x_array_size); 我需要写一个variadic模板函数h来做到这一点: template<typename… Args> void h(Args… args) { constexpr size_t nargs = sizeof…(args); // get number of args X x_array[nargs]; // create X array of that size for (int i = 0; […]

如何在C ++ 11中实现make_unique函数?

我的编译器不支持make_unique。 如何写一个? template< class T, class… Args > unique_ptr<T> make_unique( Args&&… args );

非等级rvalues总是有cv不合格的types

§3.10第9节规定:“非阶级的价值总是有cv不合格的types”。 这让我想知道 int foo() { return 5; } const int bar() { return 5; } void pass_int(int&& i) { std::cout << "rvalue\n"; } void pass_int(const int&& i) { std::cout << "const rvalue\n"; } int main() { pass_int(foo()); // prints "rvalue" pass_int(bar()); // prints "const rvalue" } 根据标准,对于非typestypes来说,不存在常量右值,但bar()更喜欢绑定到const int&& 。 这是一个编译器错误? 编辑:显然, this也是一个const rvalue 🙂 编辑:这个问题似乎是修复在g […]

括号内的初始化列表构造函数

我有以下构造函数的类Phenotype: Phenotype(uint8 init[NUM_ITEMS]); 我可以像这样创build一个表型: uint8 data[] = {0,0,0,0,0}; Phenotype p(data); 但是当我尝试创build一个像这样的时候,我得到一个错误: Phenotype p = {0,0,0,0,0}; 输出: $ make g++ -Wall -g main.cpp -std=c++0x main.cpp: In function 'int main(int, char**)': main.cpp:109: error: no matching function for call to 'Phenotype::Phenotype(<brace-enclosed initializer list>)' main.cpp:37: note: candidates are: Phenotype::Phenotype(uint8*) 该错误似乎表明有一种方法来定义一个构造函数,该构造函数接受一个大括号包含的初始化程序列表。 有谁知道这可能会怎么做?

为什么在C ++中没有放置删除expression式?

为什么C ++没有放置删除直接对应于新的位置,即调用析构函数并调用适当的位置删除操作符? 例如: MyType *p = new(arena) MyType; … //current technique p->~MyType(); operator delete(p, arena); //proposed technique delete(arena) p;

dynamicalignment的内存分配在C + + 11

Windows上的posix_memalign和_aligned_malloc允许dynamic分配alignment的内存块。 在C ++ 11中有没有类似的东西? 据我所知, alignas关键字只适用于静态分配的对象。

最简单最新的c ++ 11 ScopeGuard

我试图写一个基于Alexandrescu概念的简单的ScopeGuard,但用c ++ 11的习惯用法。 namespace RAII { template< typename Lambda > class ScopeGuard { mutable bool committed; Lambda rollbackLambda; public: ScopeGuard( const Lambda& _l) : committed(false) , rollbackLambda(_l) {} template< typename AdquireLambda > ScopeGuard( const AdquireLambda& _al , const Lambda& _l) : committed(false) , rollbackLambda(_l) { _al(); } ~ScopeGuard() { if (!committed) rollbackLambda(); } inline void […]