[英]resolving C++ operator new ambiguity (how to force it)?
我的代碼(尚未發布,在文件iaca.hh
)位於pastebin上:Basile Iaca C ++新沖突我在Debian / Linux / Sid AMD64上使用C ++ 11方言使用GCC 4.6。
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;
}
據我所知,總是以sizeof(* this)作為隱式第一個參數調用new運算符,等等。
關於我的問題的一些動機是關於Boehm的GC&C ++的 。 請不要告訴我,我不應該使用Boehm的GC。 我需要使用它(否則我將不使用C ++)。
什么是強制調用良好運算符new的正確語法?
我希望使用Boehm的GC分配器。 它可通的gc_cleanup
類,它是class IaItemValue : public IaAnyValue, gc_cleanup
,也為new
在我的class IaAnyValue
(唯一的超一流的IaDictionnaryItem
)我同意有歧義,我只是不猜語法強迫它。
那么,如何在文件末尾編碼我平凡的IaDictionnaryItem::make
使其成功編譯?
為了提高可讀性,我更喜歡使用sz=sizeof(*this)
即sizeof(IaDictionnaryItem)
, gap=0
和al=allocate_new_value
調用IaAnyValue::operator new
。 我只是想不通的語法,迫使這個特殊的被稱為(當然,我還是想的構造IaDictionnaryItem
里面被稱為IaDictionnaryItem::make
)。
我試圖使用以下命令強制gc::operator new(size_t size, GCPlacement gcp)
( gc
類由gc_cleanup
從<gc/gc_cpp.h>
繼承)
IaDictionnaryItem* IaDictionnaryItem::make() {
return new (UseGC) IaDictionnaryItem;
}
我的make
函數是一個靜態函數,它返回對象的新實例。 請記住,我使用的是Beohm的GC,當沒有指針指向它時,它將最終釋放所使用的內存(並delete
對象)。
但是我仍然
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;
}
您的問題是多重繼承問題。 注意,類IaItemValue
從IaAnyValue
和gc_cleanup
繼承(而IaDictionnaryItem
從IaItemValue
繼承),這兩個類都為operator new
提供重載。 為了解決此問題,您可以IaDictionnaryItem
添加到IaDictionnaryItem
的類定義中:
using IaAnyValue::operator new;
為了理解您的問題,我仔細閱讀了您的代碼,可以做出以下斷言:
IaAnyValue::allocate_new_value
是一個struct allocate_new_value_st {}
GCPlacement
是一個enum { NoGC, PointerFreeGC }
您有三個定義的new
運算符:
void* operator new(size_t);
void* operator new(size_t, GCPlacement);
void* operator new(size_t, void*);
您需要具體說明您所稱的內容。
調用new(size_t)
:
new X;
調用new(size_t, GCPlacement)
:
new(NoGC) X;
調用new(size_t, void*)
:
allocate_new_value_st Y;
new(&Y) X;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.