简体   繁体   English

BOOST_FUSION_ADAPT_TPL_STRUCT 与模板成员

[英]BOOST_FUSION_ADAPT_TPL_STRUCT with template members

I am new to StackOverflow and new to writing macro in c++, please forgive me if this question is too naive.我是 StackOverflow 的新手,也是用 C++ 编写宏的新手,如果这个问题太幼稚,请见谅。

I have written a template class like this:我写了一个这样的模板类:

    template<typename T, typename U>
    class Foo
    {
    public:
      unsigned n;
      std::string s;
      std::map<T,U> m;
    }

and I want to make it as fusion using the BOOST_FUSION_ADAPT_TPL_STRUCT:我想使用 BOOST_FUSION_ADAPT_TPL_STRUCT 将其作为融合:

    BOOST_FUSION_ADAPT_TPL_STRUCT
    (
      (T)(U),
      (FOO)(T)(U),
      (unsigned, n),
      (std::string, s),
      (std::map<T,U>, m)
    )

I know this does not work as the comma inside map<T,U> will cause trouble.我知道这不起作用,因为map<T,U>的逗号会引起麻烦。 If my class is not a template class, I can do typedef std::map<T,U> M inside Foo and inside the BOOST_FUSION_ADAPT_STRUCT I can write (Foo::M, m) .如果我的类不是模板类,我可以在FooBOOST_FUSION_ADAPT_STRUCT内部执行typedef std::map<T,U> M我可以写(Foo::M, m) However, as Foo is a template class, I do not know how to make it work.但是,由于Foo是一个模板类,我不知道如何使其工作。

I saw online that there are some tricks to prevent comma treated as separator in macro.我在网上看到有一些技巧可以防止将逗号视为宏中的分隔符。 For example, some people use #define COMMA , and use COMMA in std::map<T COMMA U> .例如,有些人使用#define COMMA ,并在std::map<T COMMA U>使用COMMA Yet this does not work here.然而,这在这里不起作用。 I am not very sure but I think the COMMA has already converted to "," before going through other BOOST macros called by the BOOST_FUSION_ADAPT_TPL_STRUCT.我不太确定,但我认为在通过 BOOST_FUSION_ADAPT_TPL_STRUCT 调用的其他 BOOST 宏之前, COMMA已经转换为“,”。 Some people suggest using parenthesis to encapsulate the type.有人建议使用括号来封装类型。 But this does not work here as well as this requires the macro to handle the parenthesis and extract the type.但这在这里不起作用,因为这需要宏来处理括号并提取类型。 (I am really not familiar with macro so please let me know if I am wrong) (我真的不熟悉宏,所以如果我错了,请告诉我)

Any help would be greatly appreciated!任何帮助将不胜感激! Thank you very much.非常感谢。

#include <boost/utility/identity_type.hpp>

BOOST_FUSION_ADAPT_TPL_STRUCT
(
  (T)(U),
  (Foo)(T)(U),
  (unsigned, n),
  (std::string, s),
  (typename BOOST_IDENTITY_TYPE((std::map<T,U>)), m)
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^            ~^^~  
)

DEMO演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM