相关疑难解决方法(0)

最终C++书籍指南和列表

这个问题试图收集每年出版的数十本不良C++书籍中的少数珍珠.

与许多其他编程语言不同,这些编程语言经常从互联网上的教程中随处获取,很少有人能够快速学习C++,而无需学习编写精良的C++书籍.这样做太复杂了.事实上,它是如此庞大和复杂,有很多非常糟糕的C++书籍.我们并不是在谈论糟糕的风格,而是体育明显的事实错误促进糟糕的编程风格.

请编辑接受的答案,以提供高质量的书籍和近似的技能水平 - 最好 C++聊天室讨论您的添加.(如果他们不同意建议,常客可能会毫不留情地撤销你的工作.)添加一篇关于你亲自阅读/受益的每本书的简短描述/描述.随意讨论质量,标题等.符合标准的书籍将被添加到列表中.由C和C++用户协会(ACCU)撰写评论的图书都有指向评论的链接.

*注意:常见问题和其他资源可以在C++标签信息.

c++ c++-faq

4246
推荐指数
1
解决办法
224万
查看次数

std :: move()如何将值传递给RValues?

我发现自己并没有完全理解它的逻辑std::move().

起初,我用谷歌搜索它,但似乎只有关于如何使用的文件std::move(),而不是它的结构如何工作.

我的意思是,我知道模板成员函数是什么,但是当我std::move()在VS2010中查看定义时,它仍然令人困惑.

std :: move()的定义如下.

template<class _Ty> inline
typename tr1::_Remove_reference<_Ty>::_Type&&
    move(_Ty&& _Arg)
    {   // forward _Arg as movable
        return ((typename tr1::_Remove_reference<_Ty>::_Type&&)_Arg);
    }
Run Code Online (Sandbox Code Playgroud)

首先对我来说奇怪的是参数,(_Ty && _Arg),因为当我调用函数时,如下所示,

// main()
Object obj1;
Object obj2 = std::move(obj1);
Run Code Online (Sandbox Code Playgroud)

它基本上等于

// std::move()
_Ty&& _Arg = Obj1;
Run Code Online (Sandbox Code Playgroud)

但正如您已经知道的那样,您不能直接将LValue链接到RValue引用,这让我觉得它应该是这样的.

_Ty&& _Arg = (Object&&)obj1;
Run Code Online (Sandbox Code Playgroud)

但是,这是荒谬的,因为std :: move()必须适用于所有值.

所以我想要完全理解它是如何工作的,我也应该看看这些结构.

template<class _Ty>
struct _Remove_reference
{   // remove reference
    typedef _Ty _Type;
};

template<class _Ty>
struct _Remove_reference<_Ty&>
{   // remove reference
    typedef _Ty _Type;
}; …
Run Code Online (Sandbox Code Playgroud)

c++ move-semantics c++11

92
推荐指数
1
解决办法
2万
查看次数

Initializer-list-构造不可复制(但可移动)对象的向量

可以将push_back不可复制但可移动类型的rvalues转换为该类型的向量:

#include <vector>

struct S
{
    S(int);
    S(S&&);
};

int main()
{
    std::vector<S> v;
    v.push_back(S(1));
    v.push_back(S(2));
    v.push_back(S(3));
}
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试初始化列表构造具有相同rvalues的向量时,我得到关于所需的复制构造函数的错误:

#include <vector>

struct S
{
    S(int);
    S(S&&);
};

int main()
{
    std::vector<S> v = {S(1), S(2), S(3)};
}
Run Code Online (Sandbox Code Playgroud)

我在GCC 4.7中遇到以下错误:

In file included from include/c++/4.7.0/vector:63:0,
                 from test.cpp:1:
include/c++/4.7.0/bits/stl_construct.h: In instantiation of 'void std::_Construct(_T1*, _Args&& ...) [with _T1 = S, _Args = {const S&}]':
include/c++/4.7.0/bits/stl_uninitialized.h:77:3:   required from 'static _ForwardIterator std::__uninitialized_copy<_TrivialValueTypes>::__uninit_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = const S*, _ForwardIterator = S*, …
Run Code Online (Sandbox Code Playgroud)

c++ vector initializer-list move-semantics c++11

22
推荐指数
4
解决办法
3305
查看次数

移动语义std :: move

我不太了解这个std::move功能

template <class T>
typename remove_reference<T>::type&&
move(T&& a)
{
    return a;
}
Run Code Online (Sandbox Code Playgroud)

为什么remove_reference?有人可以给我一个简单的解释吗?

c++ move-semantics c++11

12
推荐指数
2
解决办法
806
查看次数

什么是标准的魔力:移动

以下代码使VC2010失败:

//code1
std::string& test1(std::string&& x){
  return x;
}
std::string str("xxx");
test1(str);  //#1 You cannot bind an lvalue to an rvalue reference

//code2 
std::string&& test1(std::string&& x){
  return x;  //#2 You cannot bind an lvalue to an rvalue reference
}
Run Code Online (Sandbox Code Playgroud)

有一些文章可以解释#1,但我不明白为什么#2也会失败.

让我们看看std :: move是如何实现的

template<class _Ty> inline
    typename tr1::_Remove_reference<_Ty>::_Type&&
        move(_Ty&& _Arg)
    {   // forward _Arg as movable
    return ((typename tr1::_Remove_reference<_Ty>::_Type&&)_Arg);
    }
Run Code Online (Sandbox Code Playgroud)
  1. move的参数仍然是右值引用,但move(str)是可以的!
  2. move也返回rvalue.

什么是std的魔力:移动?

谢谢

c++ rvalue-reference c++11

5
推荐指数
1
解决办法
2190
查看次数