简体   繁体   中英

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.

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
    (
      (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. 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) . However, as Foo is a template class, I do not know how to make it work.

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> . 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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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