我在这里看到了这一点: Move Constructor调用基类Move Constructor
有人能解释一下:
std::move和std::forward,优选用一些代码示例?什么是std :: function <>和标准函数指针之间的区别?
那是:
typedef std::function<int(int)> FUNCTION;
typedef int (*fn)(int);
Run Code Online (Sandbox Code Playgroud)
它们实际上是一回事吗?
我一直在制作这样的文件:订单有意义吗?或者应该交换名称空间和#includes以及原因.
#ifndef CLASSNAME_H // header guards
#define CLASSNAME_H
#include "a.h" // includes in alphabetical order
#include "b.h" // user specified includes first
#include "c.h"
#include <vector> // then library includes
namespace MyNamespace
{
class ClassName
{
};
}
#endif
Run Code Online (Sandbox Code Playgroud) 基本上我想这样做: 我可以使用lambda函数或std :: function对象代替函数指针吗?
显然,对于期望函数指针的函数来说,现在是不可能的.但是,它适用于需要仿函数的函数(我之前使用过stl的sort()函数)
但是,我不知道如何编写一个以仿函数为参数的函数!
任何人?
我提到这个问题: 什么是复制和交换习惯用法?
实际上,上述答案导致以下实施:
class MyClass
{
public:
friend void swap(MyClass & lhs, MyClass & rhs) noexcept;
MyClass() { /* to implement */ };
virtual ~MyClass() { /* to implement */ };
MyClass(const MyClass & rhs) { /* to implement */ }
MyClass(MyClass && rhs) : MyClass() { swap(*this, rhs); }
MyClass & operator=(MyClass rhs) { swap(*this, rhs); return *this; }
};
void swap( MyClass & lhs, MyClass & rhs )
{
using std::swap;
/* to implement */
//swap(rhs.x, lhs.x);
} …Run Code Online (Sandbox Code Playgroud) 假设我有一个套接字:
std::shared_ptr<tcp::socket> socket( new tcp::socket(acceptor.get_io_service()) );
acceptor.async_accept( *socket, std::bind( handleAccept, this, std::placeholders::_1, socket, std::ref(acceptor)) );
Run Code Online (Sandbox Code Playgroud)
我将weak_ptr存储在容器中的所述套接字中.我需要这个,因为我想允许客户端请求其他客户端列表,以便他们可以相互发送消息.
clients_.insert(socket); // pseudocode
Run Code Online (Sandbox Code Playgroud)
然后我运行一些异步操作
socket->async_receive( boost::asio::buffer(&(*header), sizeof(Header))
, 0
, std::bind(handleReceiveHeader, this, std::placeholders::_1, std::placeholders::_2, header, socket));
Run Code Online (Sandbox Code Playgroud)
如何检测连接何时关闭,以便从容器中取出插座?
clients_.erase(socket); // pseudocode
Run Code Online (Sandbox Code Playgroud) 如果所述枚举仅用于类成员函数,是否应该在类内部或外部声明枚举?
namespace nspace
{
// need to append OC, as this pollutes the current namespace
enum OUTSIDE_CLASS {OC_POINTS, OC_LINES, OC_LINE_LOOP, :::};
enum OTHER_ENUM {OE_POINTS};
class VertexBuffer
{
public:
enum INSIDE_CLASS {POINTS, LINES, LINE_LOOP, :::};
void foo(OUTSIDE_CLASS e);
void bar(INSIDE_CLASS e);
}
};
// usage
nspace::VertexBuffer v;
v.foo(nspae::VB_POINTS);
v.bar(nspace::VertexBuffer::POINTS); // more pedantic
Run Code Online (Sandbox Code Playgroud) 以下代码不会编译:
const int a = 0;
struct Test
{
int b;
};
static const struct Test test =
{
a
};
Run Code Online (Sandbox Code Playgroud)
它是我真正想要做的一个减少的例子,但我做错了什么?
我收到以下错误:
fatal error C1107: could not find assembly 'platform.winmd': please specify the assembly search path using /AI or by setting the LIBPATH environment variable
Run Code Online (Sandbox Code Playgroud)
重现步骤
0)创建一个新的空项目
1)C/C++>常规>使用Windows运行时扩展>是
2)C/C++>代码生成>启用最小重建>否
3)添加源文件*.cpp,文件可以为空
4)尝试编译
我试图手动比较和更改项目设置,以匹配一些示例代码,但似乎没有任何工作.
c++ ×9
c++11 ×3
namespaces ×2
boost ×1
boost-asio ×1
c ×1
coding-style ×1
enums ×1
sockets ×1
swap ×1
tcp ×1
visual-c++ ×1