Re: using nested types for custom allocator of incomplete type
* abir, on 18.06.2010 14:00:
I have an custom allocator implementation whose base type depends on
completeness of the type parameter T, though the nested types doesn't
depend on so.
e.g.
template<typename T>
struct is_Small
{
const static bool value = (sizeof(T)< sizeof(void*) );
};
Probably you mean "<=" here, not "<".
template<typename T> struct impl1{};
template<typename T> struct impl2{};
template<typename T>
struct my_alloc : private select<is_small<T> , impl1<T> , impl2<T>
::type
{
typedef T* pointer;
...
};
struct foo; //incomplete type.
typedef my_alloc<foo>::pointer foo_ptr;
The same problem are supposed to happen for stack_alloc<T,N> also, as
usually they has std::aligned_storage as memory.
What is the problem?
You can not determine the size of an incomplete type and it makes no sense to
try to allocate storage to hold an instance of such a type.
Also, what is 'stack_alloc<T, N>'?
So far with std::allocator these wasn't a problem though it's allocate
function is usually
return operator new (sizeof(T) *n);
I can pull the traits in a separate class as
template<typename T>
struct alloc_traits
{
typedef T* pointer ; ...
};
and use it as
typedef alloc_traits<foo>::pointer foo_ptr;
But then I need to change the code everywhere.
Why would you do that?
Is it possible to design my_alloc such as the nested types which are
independent can still work where rest of functionality depends on
completeness of T ?
Please provide a complete example exhibiting the problem.
Copy and paste the code, do not retype it manually.
The FAQ item about how to post a question about Code That Does Not Work provides
some good advice for formulating your question in a way so that it might be
answered.
Or even is it possible to define some lazy_typedef which don't try to
instantiate the class.
You can always specialize your 'is_Small' for any type, even an incomplete one.
But it makes no sense to try to allocate storage for an instance of an
incomplete type.
I need the pointers to be defined for incomplete type for any
allocator, as otherwise many cyclic dependency will happen.
Cheers & hth.,
- Alf
--
blog at <url: http://alfps.wordpress.com>