English 中文(简体)
如何解决C++操作者的新模棱两可(如何强制使用)?
原标题:resolving C++ operator new ambiguity (how to force it)?

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: c以及idates 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)

我可以列举强迫<条码>使用新IaAnyValue将刊登在我的职衔<代码>上。 IaDictionnaryItem:make at the end of my iaca.h 档案

我先介绍了空洞的<代码>alloate_new_ Value/code>,以解决含糊不清之处,而没有成功。

我没有成功

IaDictionnaryItem* IaDictionnaryItem::make() {
  return new(IaAnyValue::allocate_new_value) IaDictionnaryItem;
}

以及

IaDictionnaryItem* IaDictionnaryItem::make() {
  return IaAnyValue::operator new(IaAnyValue::allocate_new_value) IaDictionnaryItem;
}

根据我所知,新运营商总是被称之为“规模(*)”作为隐含的第一论点。

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++).

究竟是哪一个右派把好经营者称作新经营者?

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 以及 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.

因此,我如何制定我三边的<代码>。 IaDictionnaryItem:make, at the end of the file to have it prepared better?

For readability, I would prefer calling IaAnyValue::operator new with sz=sizeof(*this) i.e. sizeof(IaDictionnaryItem), gap=0 以及 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 vetries to force the gc:operator new (size_tume, GCPlacement gcp) (gc 由<代码>gc_cleanup从<gc/gc_cpp.h>继承。

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, 以及 it will eventually release the memory used (以及 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: c以及idates 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)

在这方面。

http://www.un.org。 问题的例子很简单:

#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;
}
最佳回答

Your problem is a multiple inheritance issue. Notice that the class IaItemValue inherits from both IaAnyValue and gc_cleanup (and IaDictionnaryItem inherits from IaItemValue), both of which provide overloads for operator new. In order to resolve this, you can add the following line to the class definition for IaDictionnaryItem:

<编码>使用IaAnyValue:operator new;

问题回答

In order to understand your problem, I ve dug through your code and can make the following assertions:

  • IaAnyValue::allocate_new_value is a struct allocate_new_value_st {}
  • GCPlacement is an enum { NoGC, PointerFreeGC }

You have three defined new operators:

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;




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签