class allocators should be more encapsulated

BCS nothing at pathlink.com
Fri Dec 29 09:57:55 PST 2006


Benji Smith wrote:
> Luís Marques wrote:
>> Hello all.
>>
>> I need to build a class for which there should be only one instance 
>> with a given attribute (of type char[]).
> 
> Isn't this the standard idiom for this kind of keyed singleton pattern?
> 
[...]
> 
> I don't see why a custom allocator ever needs to be involved.
> 
> --benji

I think what would be needed is a total replacement for the creation of 
an object. This would require that this:

new ClassName(agrs);

translate to something like this:

ClassName.opTotalNewReplacment(args);

I think that what Luis is looking for is the means to use a singleton 
class with the standard new syntax. This would be nice for things like 
templates.

template Foo(T)
{
	class Foo
	{
		T[] t;
		this()
		{
			t.length = 1;
			t[0] = new T;	// <<<<<

		//this template can't be used unless new type
		// construction is allowed
		}
	}
}

That said, I don't yet have an opinion on if this is a good idea.



More information about the Digitalmars-d mailing list