0
votes

I am trying to do an excersize bu using SFINAE, but I obtain a compile error that I do not understand.

I have the following code:

#include <cstddef>                      // for size_t & NULL
#include <memory>
#include <cstdlib>
#include <iostream>
#include <type_traits>

namespace more_container
{            
    template <typename T>
    class raw_doubly_list
    {
        public:
            template<typename T1, typename T2>
            friend struct std::is_same;
        public:
            template <typename V>
            struct elem_base
            {
            };  // struct elem_base              
        public:
            #ifdef SMART_PTR
                typedef std::shared_ptr<elem_base<T> > list_type;                            
            #elif defined RAW_PTR
                typedef elem_base<T>* list_type;
            #endif                    
        public:
            typedef T value_type;

            struct delete_elem
            {

            };  // struct delete_elem            
        private: 

/*
typename std::enable_if< std::is_same< list_type,std::shared_ptr > >::value,list_type >::type ei_alloc_memory(void* p_void,const list_type& p_temp) { return temp(static_cast*>(new (p_void ) elem_base(p_temp->m_object)),delete_elem()); } */

            typename std::enable_if< std::is_same< list_type,elem_base<T>* >::value,list_type >::type
            ei_alloc_memory(void* p_void,const list_type& p_temp)
            {
                return static_cast<elem_base<T>*>(new (p_void) elem_base<T>(p_temp->m_object));
            }  

            list_type alloc_memory(const list_type& p_temp);            
        private:                     
            list_type p_to_head;
    };  // class raw_doubly_list

    //alloca memory
    template <typename T>
    typename raw_doubly_list<T>::list_type raw_doubly_list<T>::alloc_memory(const list_type& p_temp)
    {
        list_type ret = NULL;
        void* p_void = malloc(sizeof(elem_base<T>));  
        return ei_alloc_memory(p_void,p_temp);
    }    
}

    int main()
    { 
        return 0;
    }

If I understood well, enable_if returns the second template parameter if the first is true. Otherwise it fails. Then for a template member function, the signature is formed by input paramenters and the return value.

If I use the commented function I obtain a compile error (cannot be overloaded). Why ? Its signature should be different from the other because the return type should be different.

Thanks in advance.

For test use:

g++ -Wall -std=c++0x -DRAW_PTR test6.cpp

Complete error message:

test6.cpp:63:13: error: ‘typename std::enable_if::elem_base*, more_container::raw_doubly_list::elem_base*>::value, more_container::raw_doubly_list::elem_base*>::type more_container::raw_doubly_list::ei_alloc_memory(void*, more_container::raw_doubly_list::elem_base* const&)’ cannot be overloaded ei_alloc_memory(void* p_void,const list_type& p_temp) ^ test6.cpp:57:13: error: with ‘typename std::enable_if::elem_base*, std::shared_ptr::elem_base > >::value, more_container::raw_doubly_list::elem_base*>::type more_container::raw_doubly_list::ei_alloc_memory(void*, more_container::raw_doubly_list::elem_base* const&)’ ei_alloc_memory(void* p_void,const list_type& p_temp)

1

1 Answers

0
votes

You missed to specify the template parameter for the shared pointer, changed it to:

typename std::enable_if< std::is_same<list_type,std::shared_ptr<elem_base<T>>>::value>::type

(also the implementation of ei_alloc_memory has some syntax errors)