解决 C++运算符新的歧义(如何强制它)?
我的代码(尚未发布,在文件 iaca.hh
中)位于 pastebin:Basile Iaca C++ new冲突 我在 Debian/Linux/Sid AMD64 上使用 GCC 4.6 和 C++11 方言。
g++ -std=c++0x -Wall -Wextra -g -O -flto -I/usr/local/include -o iaca.hh.gch iaca.hh
iaca.hh:631:2: warning: #warning should implement the methods [-Wcpp]
iaca.hh: In static member function 'static IaDictionnaryItem* IaDictionnaryItem::make()':
iaca.hh:964:46: error: request for member 'operator new' is ambiguous
/usr/local/include/gc/gc_cpp.h:304:14: error: candidates are: static void* gc::operator new(size_t, void*)
/usr/local/include/gc/gc_cpp.h:296:14: error: static void* gc::operator new(size_t, GCPlacement)
/usr/local/include/gc/gc_cpp.h:293:14: error: static void* gc::operator new(size_t)
iaca.hh:675:7: error: static void* IaAnyValue::operator new(size_t, IaAnyValue::allocate_new_value_st)
iaca.hh:667:7: error: static void* IaAnyValue::operator new(size_t, size_t, IaAnyValue::allocate_new_value_st)
我不知道强制 operator new
的正确语法是什么。我希望在我的 iaca.hh
文件末尾的函数 IaDictionnaryItem::make
中调用我的类 IaAnyValue
中的一个函数
。我们引入了空的 allocate_new_value
来解决歧义,但没有成功。
我尝试没有成功
IaDictionnaryItem* IaDictionnaryItem::make() {
return new(IaAnyValue::allocate_new_value) IaDictionnaryItem;
}
,
IaDictionnaryItem* IaDictionnaryItem::make() {
return IaAnyValue::operator new(IaAnyValue::allocate_new_value) IaDictionnaryItem;
}
据我所知,运算符 new 总是以 sizeof (*this) 作为隐式第一个参数来调用,等等。
关于我的问题的一些动机位于 这个关于 Boehm 的 GC & 的问题C++。请不要告诉我我不应该使用 Boehm 的 GC。我需要使用它(否则我不会使用C++)。
强制调用良好的运算符 new 的正确语法是什么?
我希望使用 Boehm 的 GC 分配器。它可以通过 gc_cleanup
类获得,该类位于 class IaItemValue : public IaAnyValue, gc_cleanup
中,也可以作为我的 class 中的
(new
来使用。 IaAnyValueIaDictionnaryItem
的唯一超类)我同意存在歧义,我只是不猜测强制它的语法。
那么我应该如何在文件末尾编写我的简单 IaDictionnaryItem::make
才能成功编译它?
为了便于阅读,我更喜欢使用 sz=sizeof(*this)
调用 IaAnyValue::operator new
,即 sizeof(IaDictionnaryItem)
, gap=0
和 al=allocate_new_value
。我只是无法弄清楚强制调用这个特定函数的语法(当然,我仍然希望在 IaDictionnaryItem::make
内部调用 IaDictionnaryItem
的构造函数)。
我尝试强制使用 gc::operator new(size_t size, GCPlacement gcp)
(gc
类由 gc_cleanup
继承)来自
),使用
IaDictionnaryItem* IaDictionnaryItem::make() {
return new (UseGC) IaDictionnaryItem;
}
我的 make
函数是一个静态函数,返回我的对象的新实例。请记住,我使用的是 Beohm 的 GC,当没有指针指向它时,它最终会释放所使用的内存(并删除
对象)。
但我仍然收到
iaca.hh: In static member function 'static IaDictionnaryItem* IaDictionnaryItem::make()':
iaca.hh:962:22: error: request for member 'operator new' is ambiguous
/usr/local/include/gc/gc_cpp.h:304:14: error: candidates are: static void* gc::operator new(size_t, void*)
/usr/local/include/gc/gc_cpp.h:296:14: error: static void* gc::operator new(size_t, GCPlacement)
/usr/local/include/gc/gc_cpp.h:293:14: error: static void* gc::operator new(size_t)
iaca.hh:675:7: error: static void* IaAnyValue::operator new(size_t, IaAnyValue::allocate_new_value_st)
iaca.hh:667:7: error: static void* IaAnyValue::operator new(size_t, size_t, IaAnyValue::allocate_new_value_st)
问候。
编辑:这是问题的简单示例:
#include <cstddef>
struct A {};
struct B { void* operator new(std::size_t, A); };
struct C {};
struct D { void* operator new(std::size_t, C); };
struct E : B, D {};
int main()
{
//I want to use `void* D::operator new(std::size_t, C);` to allocate memory
//but it will not compile because the call to operator new is ambiguous.
new(C()) E;
}
My code (not yet released, in file iaca.hh
) is on pastebin: Basile Iaca C++ new conflict
I'm using GCC 4.6 on Debian/Linux/Sid AMD64 with the C++11 dialect.
g++ -std=c++0x -Wall -Wextra -g -O -flto -I/usr/local/include -o iaca.hh.gch iaca.hh
iaca.hh:631:2: warning: #warning should implement the methods [-Wcpp]
iaca.hh: In static member function 'static IaDictionnaryItem* IaDictionnaryItem::make()':
iaca.hh:964:46: error: request for member 'operator new' is ambiguous
/usr/local/include/gc/gc_cpp.h:304:14: error: candidates are: static void* gc::operator new(size_t, void*)
/usr/local/include/gc/gc_cpp.h:296:14: error: static void* gc::operator new(size_t, GCPlacement)
/usr/local/include/gc/gc_cpp.h:293:14: error: static void* gc::operator new(size_t)
iaca.hh:675:7: error: static void* IaAnyValue::operator new(size_t, IaAnyValue::allocate_new_value_st)
iaca.hh:667:7: error: static void* IaAnyValue::operator new(size_t, size_t, IaAnyValue::allocate_new_value_st)
I can't figure out what is the right syntax for forcing operator new
. I want the one from my class IaAnyValue
to be called in my function IaDictionnaryItem::make
at the end of my iaca.hh
file
I've introduced the empty allocate_new_value
to solve the ambiguity, without success.
I tried without success
IaDictionnaryItem* IaDictionnaryItem::make() {
return new(IaAnyValue::allocate_new_value) IaDictionnaryItem;
}
and
IaDictionnaryItem* IaDictionnaryItem::make() {
return IaAnyValue::operator new(IaAnyValue::allocate_new_value) IaDictionnaryItem;
}
From what I know, the operator new is always called with sizeof (*this) as implicit first argument, etc.
Some motivations about my question is in this question about Boehm's GC & C++. Please, don't tell me I should not use Boehm's GC. I need to use it (otherwise I won't use C++).
What is the right syntax to force the good operator new to be called?
I want the Boehm's GC allocator to be used. It is available thru the gc_cleanup
class, which is in class IaItemValue : public IaAnyValue, gc_cleanup
and also as the new
in my class IaAnyValue
(the only super-class of IaDictionnaryItem
) I agree there is ambiguity, I just don't guess the syntax to force it.
So how should I code my trivial IaDictionnaryItem::make
at the end of the file to get it compiled successfully?
For readability, I would prefer calling IaAnyValue::operator new
with sz=sizeof(*this)
i.e. sizeof(IaDictionnaryItem)
, gap=0
and al=allocate_new_value
. I just can't figure out the syntax to force this particular one to be called (of course, I still want the constructor of IaDictionnaryItem
to be called inside IaDictionnaryItem::make
).
I've tried to force the gc::operator new(size_t size, GCPlacement gcp)
(the gc
class is inherited by gc_cleanup
from <gc/gc_cpp.h>
) using
IaDictionnaryItem* IaDictionnaryItem::make() {
return new (UseGC) IaDictionnaryItem;
}
My make
function is a static function returning a new instance of my object. Remember that I'm using Beohm's GC, and it will eventually release the memory used (and delete
the object) when no pointers point to it.
But I'm still getting
iaca.hh: In static member function 'static IaDictionnaryItem* IaDictionnaryItem::make()':
iaca.hh:962:22: error: request for member 'operator new' is ambiguous
/usr/local/include/gc/gc_cpp.h:304:14: error: candidates are: static void* gc::operator new(size_t, void*)
/usr/local/include/gc/gc_cpp.h:296:14: error: static void* gc::operator new(size_t, GCPlacement)
/usr/local/include/gc/gc_cpp.h:293:14: error: static void* gc::operator new(size_t)
iaca.hh:675:7: error: static void* IaAnyValue::operator new(size_t, IaAnyValue::allocate_new_value_st)
iaca.hh:667:7: error: static void* IaAnyValue::operator new(size_t, size_t, IaAnyValue::allocate_new_value_st)
Regards.
EDIT: Here is simple example of the problem:
#include <cstddef>
struct A {};
struct B { void* operator new(std::size_t, A); };
struct C {};
struct D { void* operator new(std::size_t, C); };
struct E : B, D {};
int main()
{
//I want to use `void* D::operator new(std::size_t, C);` to allocate memory
//but it will not compile because the call to operator new is ambiguous.
new(C()) E;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的问题是多重继承问题。请注意,类
IaItemValue
继承自IaAnyValue
和gc_cleanup
(而IaDictionnaryItem
继承自IaItemValue
code>),两者都为operator new
提供重载。为了解决此问题,您可以将以下行添加到IaDictionnaryItem
的类定义中:using IaAnyValue::operator new;
Your problem is a multiple inheritance issue. Notice that the class
IaItemValue
inherits from bothIaAnyValue
andgc_cleanup
(andIaDictionnaryItem
inherits fromIaItemValue
), both of which provide overloads foroperator new
. In order to resolve this, you can add the following line to the class definition forIaDictionnaryItem
:using IaAnyValue::operator new;
为了理解您的问题,我深入研究了您的代码,并可以做出以下断言:
IaAnyValue::allocate_new_value
是一个struct allocate_new_value_st {}
GCPlacement< /code> 是一个
enum { NoGC, PointerFreeGC }
您定义了三个
新
运算符:您需要具体说明您所调用的内容。
调用
new(size_t)
:调用
new(size_t, GCPlacement)
:调用
new(size_t, void*)
:In order to understand your problem, I've dug through your code and can make the following assertions:
IaAnyValue::allocate_new_value
is astruct allocate_new_value_st {}
GCPlacement
is anenum { NoGC, PointerFreeGC }
You have three defined
new
operators:You need to be specific about what you call.
To call
new(size_t)
:To call
new(size_t, GCPlacement)
:To call
new(size_t, void*)
: