ICode9

精准搜索请尝试: 精确搜索
  • 构造函数中的C通用引用和返回值优化(rvo)2019-10-10 16:06:21

    为什么在具有带有通用引用参数的构造函数的类中不进行右值优化? http://coliru.stacked-crooked.com/a/672f10c129fe29a0 #include <iostream> template<class ...ArgsIn> struct C { template<class ...Args> C(Args&& ... args) {std::cout << "Ctr\n"

  • C 11:为什么右值引用参数隐式转换为左值2019-10-09 11:15:35

    以下是我的问题的简单代码, void overloaded (int&& x) { cout << "[rvalue]"; } template <class T> void fn (T&& x) { overloaded(x); } int main() { fn(0); return 0; } 我有一个编译错误 cannot bind ‘int’ lvalue to ‘int&&am

  • c – 有人可以解释有关异常的右值引用吗?2019-10-05 11:14:31

    让我说我有这个异常类: struct MyException : public std::exception { MyException(const std::exception &exc) : std::exception(exc) { cout << "lval\n"; } MyException(std::exception &&exc) : std::exception(std::forwa

  • 为什么C默认不移动构造右值引用?2019-10-02 00:06:12

    参见英文答案 > Rvalue Reference is Treated as an Lvalue?                                    4个 >            Lvalue reference constructor is called instead of rvalue reference constructor            

  • c – 当你使用std :: move返回时,“return-by-rvalue-ref”和“return-by-value”之间的区别?2019-10-01 08:06:08

    考虑以下代码: #include <iostream> using namespace std; struct I { I(I&& rv) { cout << "I::mvcotr" << endl; } }; struct C { I i; I&& foo() { return move(i) }; } }; int main() { C c; I i = c.

  • c – std :: forward如何获得正确的参数?2019-09-30 14:07:38

    考虑: void g(int&); void g(int&&); template<class T> void f(T&& x) { g(std::forward<T>(x)); } int main() { f(10); } 由于id-expression x是一个左值,并且std :: forward具有lvalues和rvalues的重载,为什么调用不会绑定到带有左值的std :: forward的重载? te

  • c – 完美转发void和non-void返回函数2019-09-30 11:06:18

    以前我用宏来测量函数调用的时间,每当我想快速检查时.现在,在C 11可用的情况下,我想最终删除预处理器代码的丑陋和平,并用以下内容替换它: template <typename Functor, typename ... Args> auto measure(Functor f, Args && ... args) -> decltype(f(std::forward<Args>(args)

  • c – 左值参考绑定的左值2019-09-30 10:08:24

    编译器一直在抱怨我试图将左值绑定到右值引用,但我看不出如何.我是C 11的新手,移动语义等,所以请耐心等待. 我有这个功能: template <typename Key, typename Value, typename HashFunction, typename Equals> Value& FastHash<Key, Value, HashFunction, Equals>::operator[](Key&

  • c – 运算符重载,允许使用rvalue进行捕获但不能分配2019-09-27 13:04:53

    是否可以设计以及如何为我的C类重载操作符以使其成为可能: C&& c = c1 + c2; 但这不可能: c1 + c2 = something; 编辑: 我把对象改成小写字母. c1,c2和c是C类的对象.&&不是逻辑运算符&&,而是rvalue引用. 例如写作: double&& d = 1.0 + 2.0; 是100%正确(新)C代码,而 1.0 + 2.0 = 4

  • c – 隐式生成的赋值运算符是否应该是&ref-qualified?2019-09-27 07:04:41

    以下代码在gcc 4.8.1上编译没有问题: #include <utility> struct foo { }; int main() { foo bar; foo() = bar; foo() = std::move( bar ); } 似乎隐含生成的foo赋值运算符不是& ref-qualified,因此可以在rvalues上调用.根据标准,这是正确的吗?如果是这样,有什么理

  • c – 我应该返回一个右值引用(通过std :: move’ing)吗?2019-09-26 19:04:27

    一个C++Next blog post说 A compute(…) { A v; … return v; } 如果A具有可访问的副本或移动构造函数,则编译器可以选择忽略该副本.否则,如果A具有移动构造函数,则移动v.否则,如果A具有复制构造函数,则复制v.  否则,将发出编译时错误. 我以为我应该总是返回没有std

  • c – 编译器是否可以自动生成std :: move以便最后使用左值?2019-09-25 21:06:21

    像这样的代码经常出现在r值引用文章中: Dave Abrams: Move It With Rvalue References void g(X); void f() { X b; g(b); // still need the value of b … g( std::move(b) ); // all done with b now; grant permission to move } 编译器是否可

  • c – 如何使模板右值引用参数仅绑定到右值引用?2019-09-23 17:14:39

    我正在编写一个网络库,并大量使用移动语义来处理文件描述符的所有权.我的一个类希望接收其他类型的文件描述符包装并取得所有权,所以就像这样 struct OwnershipReceiver { template <typename T> void receive_ownership(T&& t) { // taking file descriptor of t, and

  • 在C中,哪些类别(左值,右值,x值等)可以产生类型临时类型的表达式?2019-09-17 11:05:00

    这是一些示例代码: #include <iostream> class Foo { public: explicit Foo(int x) : data(x) {}; Foo& operator++() { data += 1; return *this; } void *get_addr() { return (void*)this; } friend Foo operator + (const Foo& lh

  • c – 为什么`std :: stringstream :: stringstream(std :: string \u0026\u0026)`不存在?2019-09-10 21:05:26

    我希望stringstream有一个构造函数可以从字符串&&中窃取它的初始内容. STL中通常不存在这样的种间“移动构造函数”吗?如果没有,为什么不呢?解决方法:有历史,这是令人失望的.但也是一个看起来光明的未来. 当移动语义进入C 11时,它是巨大的,有争议的,并且势不可挡.我希望能够将字符串

  • c – 带有左值和右值参考的std :: is_same结果2019-08-29 14:08:27

    我正在使用std :: is_same实用程序函数与rvalue和左值引用相结合,并遇到了一个奇怪的行为. 考虑这个函数模板,它检查变量t的类型. 我正在使用VS 2013: struct Test {}; template < class T> void h(T && t) { cout << " Is type &&: " << std::is_same<decltype(t), T &

  • c – 如何使用在函数中本地创建的对象而不将其复制到内存中?2019-08-24 04:05:53

    例如,我有一个类在其consturctor中调用一个返回本地对象的函数.我正在尝试使用rvalue引用来访问此对象,以避免在内存中进行昂贵的移动. class MyClass { BigObject&& C; MyClass() : C(f()) { }; }; BigObject f() { ret

  • 自动右值参考(不带\u0026\u0026)到C 14中的数组2019-08-23 21:09:12

    回复帖子1 How to declare array with auto工作正常: template<typename T> using unsized_raw_array = T[]; auto &&z = unsized_raw_array<int>{1, 2, 3}; 尝试相同但没有双&符号(&&)会导致clang和gcc的不同行为: template<typename T> using unsized_raw_ar

  • C转发参考和r值参考2019-08-23 17:07:21

    我知道转发引用是“对cv-unqualified模板参数的右值引用”,例如in template <class T> void foo(T&& ); 这意味着上述函数可以同时采用l值和r值引用. 有些东西我不明白,例如 template <class T> class A { template <class U> void foo(T&& t, U&& u) { T t

  • 在c 11中为什么在std :: move之后没有权利使用移动变量?2019-07-28 12:06:47

    因为std :: move(v)只是一个类似的演员 static_cast<T&&>(v) 其中T是v的类型.但是为什么移动的变量不是原始对象的引用? 例如,当o1“移动”到o2时,为什么我们不能再通过o1.str_访问o2的str_? #include <string> #include <iostream> struct MyClass { std::string str_; MyCl

  • c – 返回右值引用vs函数返回类型中的值返回2019-07-28 07:06:37

    参见英文答案 > c++11 Return value optimization or move?                                     4个 在我的代码中,我有一个函数,它从一段数据构造一个字符串然后返回它.此字符串不会在其他任何地方使用,因此接收方可以安全地使用移动分配或

  • c – 由于右值参考向量:: push_back函数如何提高效率而感到困惑2019-07-27 09:07:14

    在cppreference.com上,我找到了一个关于使用std :: move的简单示例: std::string str = "Hello"; std::vector<std::string> v; // uses the push_back(const T&) overload, which means // we'll incur the cost of copying str v.push_back(str);

  • c – 为什么通用参考和右值参考流的差异2019-07-24 12:06:58

    根据Efficient Modern C,第25项,我们有一个例子 情况1 class Widget { public: template<typename T> void setName(T&& newName) { name = std::forward<T>(newName); } ... }; 案例2 class Widget { public: void setName(const std::string& newName) { name =

  • 需要Meyers Effective C Widget rvalue示例说明2019-07-22 19:07:35

    我有一个小问题. 在Effective Modern C的第一页上,有一个例子: class Widget { public: Widget(Widget&& rhs); }; 此外,还有一个评论:’rhs是一个左值,虽然它有一个右值引用类型’. 说实话,我什么都不懂.这是什么意思’rhs是一个左值,但它的类型是右值参考’?解决方法:请记住,

  • c – 按值返回到右值参考2019-07-22 17:05:44

    我正在研究右值引用,我对以下代码有疑问: string func() { return "Paul"; } int main() { string&& nodanger = func(); // The lifetime of the temporary is extended // to the life-time of the reference. return 0; } 问题是:func()返回什么? 我相信这

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有