class Whatever
{
public:
virtual ~Whatever();
protected:
Whatever();
virtual void SomeMethod();
void OnEventOccurred(int x);
std::vector<boost::signals2::scoped_connection> boostSignalConnections_;
}
Run Code Online (Sandbox Code Playgroud)
// .cpp
Whatever::SomeMethod()
{
...
boostSignalConnections_.push_back(
anObject->AddEventOccurredObserver(
std::bind(&Whatever::OnEventOccurred,
this, std::placeholders::_1)));
...
}
Run Code Online (Sandbox Code Playgroud)
(注意AddEventOccurredObserver只委托boost::signals2::connect()并返回boost::signals2::connection)
我收到以下错误.很难解释模板错误,但似乎错误是在成员声明中引起的std::vector<boost::signals2::scoped_connection> boostSignalConnections_;
...\vc\include\xmemory(202): error C2248:
'boost::signals2::scoped_connection::scoped_connection' :
cannot access private member declared in class 'boost::signals2::scoped_connection'
...\boost_1_47\boost\signals2\connection.hpp(234) : see declaration of
'boost::signals2::scoped_connection::scoped_connection'
...\boost_1_47\boost\signals2\connection.hpp(210) :
see declaration of 'boost::signals2::scoped_connection'
...\vc\include\xmemory(201) : while compiling class template member function
'void std::allocator<_Ty>::construct(boost::signals2::scoped_connection *,_Ty &&)'
with
[
_Ty=boost::signals2::scoped_connection
] …Run Code Online (Sandbox Code Playgroud) 有没有办法确定某些类型在编译期间是不可复制的?我需要以下:
template<typename T, unsigned long long MaxSize>
struct circular_buffer : boost::noncopyable {
static_assert(typeof(T) ?????, "T must be noncopyable!");
};
Run Code Online (Sandbox Code Playgroud) 关于实现(编译器)不提供复制构造函数和复制赋值运算符的情况,我有一点混淆.
我对第二种情况有点困惑,第二种情况正是如此.
a)实现不会为您声明它们,因此您将收到编译时错误.
或者
b)实现将声明并定义它们,但是当编译器定义的实现试图找到基类的方法时,我们将得到编译时错误.
昨天我接受了一次采访,我说(b)正在发生,但面试官不同意,他说(a).
我尝试在Microsoft C/C++ 14.00和gcc 4.4.5中编译以下代码
struct A
{
private:
A& operator = ( const A& );
};
struct B : A
{
};
int main()
{
B b1;
B b2;
b1 = b2;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
Microsoft编译器输出
ctor01.cpp(9) : error C2248: 'A::operator =' : cannot access private member declared in class 'A'
ctor01.cpp(4) : see declaration of 'A::operator ='
ctor01.cpp(2) : see declaration of 'A'
This diagnostic occurred in the compiler generated function …Run Code Online (Sandbox Code Playgroud) 具有以下内容:
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ifstream f;
ifstream g;
f = std::move(g);
}
Run Code Online (Sandbox Code Playgroud)
为什么被称为ifstream::operator=(const ifstream&)而不是ifstream::operator=(ifstream&&)即使std::move()被称为?
更新:一般来说,有没有办法强制左值引用到右值引用?
在C#中是否有可能创建一个无法复制的类,而只能通过引用传递?
C++中的等价物是删除复制构造函数和复制赋值运算符.
我上课了
class A {
public:
A(int x): x_(x) {}
void SetValue(int m) {x_=m};
private:
DISALLOW_COPY_AND_ASSIGN(A);
};
Run Code Online (Sandbox Code Playgroud)
我正在尝试创建A类对象的向量
vector<std::unique_ptr<A>> objects;
objects.reserve(10);
for (int i = 0; i < 10; i++) {
auto a = MakeUnique<A>();
a->SetValue(20);
objects.emplace_back(a);
}
Run Code Online (Sandbox Code Playgroud)
这导致对已删除的构造函数的编译错误调用 'std::unique_ptr<A, std::default_delete<A> >'
我有一个包含潜在的顶点的Shape类,我正在考虑将copy-constructor/copy-assignment设为私有,以防止意外不必要地复制我的重量级类(例如,通过值而不是通过引用传递).
要制作Shape的副本,必须故意调用"克隆"或"复制"方法.
这是好习惯吗?我想知道为什么STL容器不使用这种方法,因为我很少想通过值传递它们.
尝试使用不同的文件名创建一些gzip存档我写下以下代码片段.
#include <iostream>
#include <utility>
#include <boost/iostreams/filtering_stream.hpp>
#include <boost/iostreams/device/file.hpp>
#include <boost/iostreams/filter/gzip.hpp>
boost::iostreams::filtering_ostream&& makeGZipStream(const std::string& archiveName,
const std::string& fileName)
{
boost::iostreams::filtering_ostream theGzipStream;
boost::iostreams::gzip_params theGzipParams;
theGzipParams.file_name = fileName;
theGzipStream.push(boost::iostreams::gzip_compressor{theGzipParams});
theGzipStream.push(boost::iostreams::file_sink{archiveName});
return std::move(theGzipStream);
}
int main()
{
boost::iostreams::filtering_ostream&& theGzipStream = makeGZipStream("archive.gz", "file");
theGzipStream << "This is a test..." << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这(我们可以预期)会产生核心转储,因为makeGZipStream我们尝试通过(rvalue-)引用返回本地堆栈分配的变量.但在这种情况下,副本不是一个选项,因为它boost::iostreams::filtering_ostream是不可复制的.
std::unique_ptr"按值"(由于copy-elision,这个移动甚至不应该出现在C++ 17中),为什么在这种情况下不可能呢?unique_ptr(不那么漂亮)使用的编译器很老了g++ (GCC) 4.9.3.