Pregunta

Estoy tratando de girar Este código/respuesta C ++ (clase que crea mapa string <-> some_array_of_predefined_function_types ) en una clase más automatizada usando Boost/Preprocessor.

He creado el siguiente código:

#include <boost/preprocessor.hpp>
#include <iostream>
#include <string>
#include <map>
#include <vector>

#define CF_ADD_MAPS(z, n, T) \
std::map<std::string, identity< BOOST_PP_CAT(T, n ) >::type* >  \
BOOST_PP_CAT(T , BOOST_PP_CAT(n , _var));  

#define CF_ADD_OPERATORS(z, n, T) \
operator identity< BOOST_PP_CAT(T, n ) >::type*()  \
{return \
BOOST_PP_CAT( p->   , BOOST_PP_CAT(T , BOOST_PP_CAT(n , BOOST_PP_CAT( _var, [n]) )) ) ; \
}

#define CF_ADD_INSERTERS(z, n, T) \
    void insert(std::string name, identity< BOOST_PP_CAT(T, n ) >::type* ptr)   \
{ \
    BOOST_PP_CAT(T , BOOST_PP_CAT(n , BOOST_PP_CAT( _var, [name]) )) =ptr; \
}

template <typename T> struct identity { typedef T type; };

#define CF_DEFINE_MAP_STRUCT_I(name, number)                   \
template <BOOST_PP_ENUM_PARAMS(number, class T)>              \
class name {                                          \
      BOOST_PP_REPEAT(number, CF_ADD_MAPS, T)        \
      friend class apitemp;                                        \
      public: \
      class apitemp {  \
        std::string n;  \
        BOOST_PP_CAT(name, *) p; \
        public: \
            apitemp(std::string name_, BOOST_PP_CAT(name, *) parent)  \
                : n(name_), p(parent) {} \
            BOOST_PP_REPEAT(number, CF_ADD_OPERATORS, T) \
      }; \
      BOOST_PP_REPEAT(number, CF_ADD_INSERTERS, T) \
      apitemp operator[](std::string n) {return apitemp(n, this);} \
};


#define CF_MAP_CLASS_NAME my_map
#define CF_GENERATE_MAP_CLASS(z, n, data) CF_DEFINE_MAP_STRUCT_I(CF_MAP_CLASS_NAME, n)
#define CF_MAX_MAP_TYPES_COUNT 2
BOOST_PP_REPEAT(CF_MAX_MAP_TYPES_COUNT, CF_GENERATE_MAP_CLASS, x)


/*
class api {
    //maps containing the different function pointers
    std::map<std::string, identity<void()>::type* > voida;
    std::map<std::string, identity<int(std::string, const int&)>::type* > stringcrint;
    friend class apitemp;
public:
    //api temp class 
    //given an api and a name, it converts to a function pointer  
    //depending on parameters used
    class apitemp {
        std::string n;
        api* p;
    public:
        apitemp(std::string name, api* parent) 
            : n(name), p(parent) {}
        operator identity<void()>::type*()
        {return p->voida[n];}
        operator identity<int(std::string, const int&)>::type*()
        {return p->stringcrint[n];}
    }; 
    //insertion of new functions into appropriate maps
    void insert(std::string name, identity<void()>::type* ptr) 
    {voida[name]=ptr;}
    void insert(std::string name, identity<int(std::string, const int&)>::type* ptr)
    {stringcrint[name]=ptr;}
    //operator[] for the name gets halfway to the right function
    apitemp operator[](std::string n) {return apitemp(n, this);}
} myMap;
*/

int hello_world(std::string name, const int & number )
{
    name += "!";
    std::cout << "Hello, " << name << std::endl;
    return number;
}

int main() {
    my_map<int(std::string, const int &)> myMap;
    myMap.insert("my_method_hello", &hello_world ); 
    int a = myMap["my_method_hello"]("Tim", 25);
    std::cout << a << std::endl;
    std::cin.get();
}

Que no se compila con los próximos 5 errores:

Error   1   error C2913: explicit specialization; 'my_map' is not a specialization of a class template  

Error   2   error C2133: 'myMap' : unknown size

Error   3   error C2512: 'my_map' : no appropriate default constructor available

Error   4   error C2039: 'insert' : is not a member of 'my_map'

Error   5   error C2676: binary '[' : 'my_map' does not define this operator or a conversion to a type acceptable to the predefined operator

Mientras que el preprocesador genera:

template <>
class my_map {
    friend class apitemp;
public:
    class apitemp {
        std::string n;
        my_map* p;
    public: apitemp(std::string name_, my_map* parent) : n(name_), p(parent) {}  
    };
    apitemp operator[](std::string n) {
        return apitemp(n, this);
    } 
};
template < class T0>
class my_map {
    std::map<std::string, identity< T0 >::type* > T0_var;
    friend class apitemp;
public:
    class apitemp {
        std::string n;
        my_map* p;
    public:
        apitemp(std::string name_, my_map* parent) : n(name_), p(parent) {} 
        operator identity< T0 >::type*() {
            return p->T0_var[0] ; 
        } 
    }; 
    void insert(std::string name, identity< T0 >::type* ptr) {
        T0_var[name] =ptr;
    } 
    apitemp operator[](std::string n) {
        return apitemp(n, this);
    } 
};

Me pregunto qué hice mal y cómo hacer que se compilara.

¿Fue útil?

Solución

template <>
class my_map {
  // ...
};

El problema está aquí con el BOOST_PP_ENUM_PARAMS, que también generará una versión de 0 param. Y desde las versiones 0-Param (template<>) Introduzca especializaciones, te errores. Corrección: tenga al menos un parámetro fijo.

Siguiente problema: no puede "sobrecargar" las clases basadas en los parámetros de la plantilla. Tienen que tener diferentes nombres o estar estructurados de otra manera con especializaciones parciales o algo así.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top