template member function's definition outside the class

If I have type alias in my class and 2 member functions
1
2
3
4
5
6
7
8
9
10
11
template<typename T> 
Pool_alloc
{
    //...
    using pointer = T*;
    using const_pointer = const T*;
    //...

    pointer allocate(size_type n);
    pointer allocate2(size_type n, typename Pool_alloc<void>::const_pointer hint = 0);
};


Pool_alloc<void> is template specialization
1
2
3
4
5
6
7
template<>
class Pool_alloc<void>
{
public:
	using const_pointer = const void*;
        //...
};


I know how to write function allocate outside its class and the ugly version looks like this and works fine
1
2
3
4
5
template<typename T>
typename Pool_alloc<T>::pointer Pool_alloc<T>::allocate(typename Pool_alloc<T>::size_type n)
{
	return reinterpret_cast<typename Pool_alloc<T>::pointer>(::new char[n * sizeof(T)]);
}


My question is how do I add this second argument in version allocate2?
I tried to insert : typename Pool_alloc<void>::const_pointer hint as 2nd argument but its not working and I'm getting
'Pool_alloc<T>::allocate2': unable to match function definition to an existing declaration

error
1
2
3
4
5
template<typename T>
typename Pool_alloc<T>::pointer Pool_alloc<T>::allocate(typename Pool_alloc<T>::size_type n, /* code for typename Pool_alloc<void>::const_pointer hint*/)
{
	return reinterpret_cast<typename Pool_alloc<T>::pointer>(::new char[n * sizeof(T)]);
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <new>

template < typename T > struct Pool_alloc ; // declare class template

template <> struct Pool_alloc<void> // define specialisation for void
{
    using pointer = void*;
    using const_pointer = const void*;
};

template < typename T > struct Pool_alloc // define the non-specialised template
{
    //...
    using pointer = T*;
    using const_pointer = const T*;
    using size_type = std::size_t ;
    //...

    pointer allocate( size_type n );
    pointer allocate2( size_type n, Pool_alloc<void>::const_pointer hint = nullptr );
};

template < typename T > typename Pool_alloc<T>::pointer Pool_alloc<T>::allocate( size_type n )
{ return static_cast<pointer>( ::operator new( n * sizeof(T) ) ) ; }

template < typename T >
typename Pool_alloc<T>::pointer Pool_alloc<T>::allocate2( size_type n, Pool_alloc<void>::const_pointer )
{ return allocate(n) ; }

http://coliru.stacked-crooked.com/a/f0e49cdec199fcc5
Your Pool_alloc<void>::const_pointer is an alias for const void *.
Why not use the latter?
Ok I tried if I create allocate2 like this and it worked

1
2
template<typename U>
pointer allocate2(size_type n, typename Pool_alloc<U>::const_pointer hint = 0);


and than outside its class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
template<typename T>
using Pointer = typename Pool_alloc<T>::pointer;

template<typename T>
using Size_type = typename Pool_alloc<T>::size_type;

template<typename T>
using Const_pointer = typename Pool_alloc<T>::const_pointer;

template<typename T>
template<typename U>
Pointer<T> Pool_alloc<T>::allocate2(Size_type<T> n, Const_pointer<U> hint)
{
	return reinterpret_cast<Pointer<T>>(::new char[n * sizeof(T)]);
}


But the problem is now I can pass all kinds of pointers as hint parameter not just const void*. How to make it so only Pool_alloc<void>::const_pointer is accepted?
Last edited on
@keskiverto

Tnx for an answer. To answer your question its because I want to understand why the 1st way its not working, its not that important to get this to run using const void* without learning anything new
Last edited on
I think we have a winner!!! :D Tnx you so much for an answer JLBorges. I can see what I did wrong and tnx for improvements in my code. Will also think about them. Thanks!
Topic archived. No new replies allowed.