c++ - Boost预处理器多个模板类生成

标签 c++ templates boost boost-preprocessor

我正在尝试转this C++ code/answer (创建 map string <-> some_array_of_predefined_function_types 的类)使用 boost/预处理器将其转换为更自动化的类。

我创建了以下代码:

#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();
}

它不会编译并出现接下来的 5 个错误:

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

当预处理器生成时:

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);
    } 
};

我想知道我做错了什么以及如何让它编译?

最佳答案

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

问题出在 BOOST_PP_ENUM_PARAMS 上,这也将生成一个 0 参数版本。由于 0 参数版本 ( template<> ) 引入了特化,它会在你身上出错。 Fix:至少有一个固定参数。

下一个问题:您不能根据模板参数“重载”类。它们必须具有不同的名称或以其他方式构建,具有部分特化之类的东西。

关于c++ - Boost预处理器多个模板类生成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8374693/

相关文章:

C++使用getline从文本文件数据类型作为结构读取

c++ - Eigen 模板库Random()方法算法

c++ - 我可以在控制流语句中使用 SFINAE 测试吗?

c++ - 为什么 C++ 更喜欢这个模板方法而不是方法重载?

c++ - 提神气从子规则赋值

c++ - 如何在 C++ 中更有效地生成这么多排列?

C++ 库交叉依赖 - 从 llvm 移植到 gcc

javascript - Meteor:在发布中使用 Meteor._sleepForMs() 时 Iron router 不显示加载模板

c++ - boost asio : different thread pool for different tasks

c++ - boost 错误 : reference to 'detail' is ambiguous