c++11专题提供c++11的最新资讯内容,帮你更好的了解c++11。
考虑一下: #include <iostream> #include <functional> std::function<void()> task; int x = 42; struct Foo { int& x; void bar() { task = [=]() { std::cout << x << '\n'; }; } }; int main(
这个C 11代码对我来说很好: #include <iostream> #include <vector> #include <array> using namespace std; struct str { int first, last; }; vector<str> fields { {1,2}, {3,4}, {5,6} }; int main() { fo
C的ostream类为运算符<<提供了许多默认重载,但它们并非都以相同的方式定义. char类型,字符串类型和rvalue流的 overloads定义为免费的命名空间范围函数,例如: namespace std { ostream &operator<<(ostream &os, char c); } 而算术类型,streambuf和流操作符的overloads被定义为std :: ostream
int v[1]; auto p1 = v; auto &p2 = v; auto *p3 = v; p1的类型为int *(对于p3是相同的).特别是在这个简单的样本中,我发现p2(int(&)[1])更有用,因为它具有特定的数组语义,例如:我可以在p2上应用sizeof,使其与v上的sizeof相同. 是否有关于此的标准报价? 为什么默认参考是一个坏主意? (对于这个数组的情况,我的意思是,
关于std :: piecewise_construct与std :: map一起使用时,我有点困惑.例: std::map<std::string, std::string> m; // uses pair's piecewise constructor m.emplace(std::piecewise_construct, std::forward_as_tuple("c
Visual C 2017和 gcc 5.4生成从’const unsigned char’到’const float’的转换需要对B行进行缩小转换警告,但在此代码段中不需要对A行进行转换: #include <iostream> int main() { const unsigned char p = 13; const float q = p; // Lin
C用户定义的文字运算符是否可以传递空指针? 这实际上发生在g的实验版本(gcc版本4.7.0 20111114(实验性)[主干版本181364](Debian 20111114-1))但我不确定这是否是一个错误(90%肯定)或一些奇怪的预期行为. 示例程序: #include <iostream> #include <stdexcept> #include <string> std::strin
我有类基,它只包含私有默认构造函数和公共删除的复制构造函数,没有别的. class base { private: base() = default; public: base(const base&) = delete; }; 如果我尝试从base继承并创建一个派生类的实例,如下所示,g 4.8.2不会编译我的代码,但VC 2013会编译. class derived : pu
我花了很多时间为我的类实现移动语义,但现在我正在处理使用它的函数. 好的,所以我有这个对象在堆上有很多数据:我实现了移动语义的CLargeOb(构造函数和运算符=).它的理想用途如下: void OtherOb::Func(CLargeOb&& largeOb1, CLargeOb&& largeOb2) { SomeOtherFunc(largeOb1); // use objects
鉴于std :: chrono :: duration可以表示两次之间的有符号差异,因此需要这种持续时间的绝对值似乎是一种非常常见的情况.例如,以下代码按预期输出diff:-5: using namespace std; using namespace std::chrono; auto now = system_clock::now(); auto then = now - seconds(5
只是为了好玩,我已经实现了可以想象的最简单的排序算法: template<typename Iterator> void treesort(Iterator begin, Iterator end) { typedef typename std::iterator_traits<Iterator>::value_type element_type; // copy data in
我正在尝试检查仿函数是否与给定的一组参数类型和给定的返回类型兼容(也就是说,给定的参数类型可以被隐含地转换为实际的参数类型,反之亦然的是返回类型).目前我使用以下代码: template<typename T, typename R, template<typename U, typename V> class Comparer> struct check_type { enum
是否有可能接受两种不同类型的lambda函数作为类成员而不知道他们的模板参数提前? struct two_functors { std::function<???> a; std::function<???> b; ... }; 这样的事情是可能的: void main(){ vector<two_functors> many_functors; int
我有一个main.cpp文件,只有这个代码: #include <iostream> #include "include/rapi/RApi.h" using namespace std; int main() { std::cout << "Test\n"; return 0; } 当我编译时,我想看到我的代码中的警告,但不是来自外部包含的文件.我以前能够实现这一点,但是我可
有没有办法从虚函数调用基类方法作为派生类,而不是作为基类? 示例代码: class A { public: virtual void a() = 0; void print() { std::cerr << typeid(decltype(*this)).name(); }; }; class B : public A { public: virtual void a()
我想通过引用传递一个struct,因此它不会被复制,但Resharper会在下面给出警告: struct sometype { }; sometype foo() { sometype x; return x; } void bar() { sometype & a = foo();//Binding r-value to l-value reference is no
我尝试用其他类型谓词来理解POD概念. 是否有完整的类型谓词列表,它们对应于std :: is_pod<>? 这是我目前的尝试不成立(VS2013). struct C { // Does not assert as POD class int value; C() = default; C(int v) : value(v) { // Offends is_pod<>
在以下代码中(为演示而简化): namespace mpl = boost::mpl; using if1 = mpl::if_<std::is_same<double, mpl::_1>, double, void>; //using if2 = mpl::if_<std::is_same<double, mpl::_1>, typename std::common_type<double
在Visual Studio 2013中,我在一个空的,全新的命令行解决方案中编写了以下内容: int main(int argc, char* argv[]) { int xs[1]; for (auto x : xs) do ; while (0); return 0; } 编译时,我收到以下错误: error C
作为一个愚蠢的例子,假设我有一个函数int f(vector< int> v),由于某种原因,我需要在f中多次对v进行几次操作.而不是在其他地方放置辅助函数(这可能会增加混乱并损害可读性),做这样的事情有什么优点和缺点(效率,可读性,可维护性等): int f(vector<int> v) { auto make_unique = [](vector<int> &v) {