尝试创建临时对象时出现奇怪的编译器错误
在我发布这个问题 我尝试重现创建作用域 RAII 对象时意外创建右值的问题。现在看来我无法在没有编译器错误的情况下重现它!
在以下代码示例中,在 Test::foo()
中,第二个 ScopedLock 创建无法编译。 gcc 编译器错误似乎完全错误。谁能解释一下吗?
struct Mutex
{
void lock() { }
void unlock() { }
};
struct ScopedLock
{
ScopedLock(Mutex & inMutex) : mMutex(inMutex)
{ mMutex.lock(); }
~ScopedLock()
{ mMutex.unlock(); }
private:
ScopedLock(const ScopedLock&);
ScopedLock& operator=(const ScopedLock&);
Mutex mMutex;
};
struct Test
{
void foo()
{
// Compiles fine
ScopedLock lock(mMutex);
// Error: no matching function for
// call to ‘ScopedLock::ScopedLock()’
ScopedLock(mMutex);
}
Mutex mMutex;
};
我在 Mac 上使用 GCC 4.2.1。
更新
我查看了原始代码,发现该成员是通过 this
指针引用的:
ScopedLock(this->mMutex); // short-lived temporary and compiles fine
After I posting this question I tried to reproduce the problem of accidental rvalue creation when creating a scoped RAII object. Now it appears that I can't reproduce it without compiler errors!
In the following code sample, in Test::foo()
the second ScopedLock creation doesn't compile. The gcc compiler error seems totally wrong. Can anyone explain?
struct Mutex
{
void lock() { }
void unlock() { }
};
struct ScopedLock
{
ScopedLock(Mutex & inMutex) : mMutex(inMutex)
{ mMutex.lock(); }
~ScopedLock()
{ mMutex.unlock(); }
private:
ScopedLock(const ScopedLock&);
ScopedLock& operator=(const ScopedLock&);
Mutex mMutex;
};
struct Test
{
void foo()
{
// Compiles fine
ScopedLock lock(mMutex);
// Error: no matching function for
// call to ‘ScopedLock::ScopedLock()’
ScopedLock(mMutex);
}
Mutex mMutex;
};
I'm using GCC 4.2.1 on Mac.
Update
I had a look at the original code and saw that the member was referenced through the this
pointer:
ScopedLock(this->mMutex); // short-lived temporary and compiles fine
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您有两个用户声明的构造函数,因此没有编译器生成的默认构造函数。
是的,
处理方式与 相同。
这样的括号在更复杂的声明中很有用,例如
声明指向返回类型的函数的指针。
You have two user declared constructors, so there is no compiler generated default one.
Yes,
is handled in the same way as
Such parenthesis are useful in more complex declarations such as
to declare a pointer to a function returning a type.
该消息告诉您 ScopedLock 没有默认构造函数,即不带参数的构造函数。如果您声明一个带有参数的构造函数,C++ 不会为您创建默认构造函数。
The message is telling you that ScopedLock doesn't have a default constructor, i.e. one that takes no arguments. If you declare a constructor that takes arguments, C++ won't create a default one for you.