diff --git a/include/boost/fusion/adapted/struct/detail/define_struct_inline.hpp b/include/boost/fusion/adapted/struct/detail/define_struct_inline.hpp index 7bc5fe08..c06b61b5 100644 --- a/include/boost/fusion/adapted/struct/detail/define_struct_inline.hpp +++ b/include/boost/fusion/adapted/struct/detail/define_struct_inline.hpp @@ -8,6 +8,7 @@ #ifndef BOOST_FUSION_ADAPTED_STRUCT_DETAIL_DEFINE_STRUCT_INLINE_HPP #define BOOST_FUSION_ADAPTED_STRUCT_DETAIL_DEFINE_STRUCT_INLINE_HPP +#include #include #include #include @@ -25,8 +26,32 @@ #include #include #include +#include #include +// MSVC and GCC <= 4.4 have a bug that affects partial specializations of +// nested templates under some circumstances. This affects the implementation +// of BOOST_FUSION_DEFINE_STRUCT_INLINE, which uses such specializations for +// the iterator class's 'deref' and 'value_of' metafunctions. On these compilers +// an alternate implementation for these metafunctions is used that does not +// require such specializations. The alternate implementation takes longer +// to compile so its use is restricted to the offending compilers. +// For MSVC, the bug was was reported at https://connect.microsoft.com/VisualStudio/feedback/details/757891/c-compiler-error-involving-partial-specializations-of-nested-templates +// For GCC, 4.4 and earlier are no longer maintained so there is no need +// to report a bug. +#if defined(BOOST_MSVC) || (defined(__GNUC__) && (__GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ <= 4))) + #define BOOST_FUSION_NEED_NESTED_TEMPLATE_PARTIAL_SPEC_WKND +#endif + +#ifdef BOOST_FUSION_NEED_NESTED_TEMPLATE_PARTIAL_SPEC_WKND +#include +#include +#include +#include +#include +#endif + + #define BOOST_FUSION_MAKE_DEFAULT_INIT_LIST_ENTRY(R, DATA, N, ATTRIBUTE) \ BOOST_PP_COMMA_IF(N) BOOST_PP_TUPLE_ELEM(2, 1, ATTRIBUTE)() @@ -148,6 +173,95 @@ #define BOOST_FUSION_MAKE_DATA_MEMBER(R, DATA, N, ATTRIBUTE) \ BOOST_PP_TUPLE_ELEM(2, 0, ATTRIBUTE) BOOST_PP_TUPLE_ELEM(2, 1, ATTRIBUTE); +#ifdef BOOST_FUSION_NEED_NESTED_TEMPLATE_PARTIAL_SPEC_WKND + +#define BOOST_FUSION_DEFINE_ITERATOR_VALUE_OF(NAME, ATTRIBUTE_SEQ_SIZE) \ + template \ + struct value_of : boost::fusion::result_of::at_c< \ + ref_vec_t, \ + boost_fusion_detail_Iterator::index::value \ + > \ + { \ + }; + +#define BOOST_FUSION_DEFINE_ITERATOR_DEREF(NAME, ATTRIBUTES_SEQ) \ + template \ + struct deref \ + { \ + typedef typename boost::remove_const< \ + boost_fusion_detail_Iterator \ + >::type iterator_raw_type; \ + \ + static const int index = iterator_raw_type::index::value; \ + \ + typedef typename boost::fusion::result_of::at_c< \ + ref_vec_t, \ + index \ + >::type result_raw_type; \ + \ + typedef typename boost::mpl::if_< \ + boost::is_const, \ + typename boost::add_const::type, \ + result_raw_type \ + >::type type; \ + \ + static type call(iterator_raw_type const& iter) \ + { \ + return boost::fusion::at_c(iter.ref_vec); \ + } \ + }; + +#define BOOST_FUSION_MAKE_ITERATOR_WKND_FIELD_NAME(R, DATA, N, ATTRIBUTE) \ + BOOST_PP_COMMA_IF(N) seq.BOOST_PP_TUPLE_ELEM(2, 1, ATTRIBUTE) + +#define BOOST_FUSION_DEFINE_ITERATOR_WKND_INIT_LIST_ENTRIES(ATTRIBUTES_SEQ) \ + , ref_vec(BOOST_PP_SEQ_FOR_EACH_I( \ + BOOST_FUSION_MAKE_ITERATOR_WKND_FIELD_NAME, \ + ~, \ + BOOST_PP_SEQ_TAIL(ATTRIBUTES_SEQ))) + +#define BOOST_FUSION_MAKE_ITERATOR_WKND_REF(Z, N, DATA) \ + BOOST_PP_COMMA_IF(N) \ + typename boost::mpl::if_< \ + boost::is_const, \ + typename boost::add_const< \ + typename boost_fusion_detail_Seq::t##N##_type \ + >::type, \ + typename boost_fusion_detail_Seq::t##N##_type \ + >::type& + +#define BOOST_FUSION_DEFINE_ITERATOR_WKND_MEMBERS(ATTRIBUTES_SEQ_SIZE) \ + typedef boost::fusion::vector< \ + BOOST_PP_REPEAT( \ + ATTRIBUTES_SEQ_SIZE, \ + BOOST_FUSION_MAKE_ITERATOR_WKND_REF, \ + ~) \ + > ref_vec_t; \ + \ + ref_vec_t ref_vec; + +#else + +#define BOOST_FUSION_DEFINE_ITERATOR_VALUE_OF(NAME, ATTRIBUTES_SEQ_SIZE) \ + template struct value_of; \ + BOOST_PP_REPEAT( \ + ATTRIBUTES_SEQ_SIZE, \ + BOOST_FUSION_MAKE_ITERATOR_VALUE_OF_SPECS, \ + NAME) + +#define BOOST_FUSION_DEFINE_ITERATOR_DEREF(NAME, ATTRIBUTES_SEQ) \ + template struct deref; \ + BOOST_PP_SEQ_FOR_EACH_I( \ + BOOST_FUSION_MAKE_ITERATOR_DEREF_SPECS, \ + NAME, \ + ATTRIBUTES_SEQ) + +#define BOOST_FUSION_DEFINE_ITERATOR_WKND_INIT_LIST_ENTRIES(ATTRIBUTES_SEQ) + +#define BOOST_FUSION_DEFINE_ITERATOR_WKND_MEMBERS(ATTRIBUTES_SEQ_SIZE) + +#endif // BOOST_FUSION_NEED_NESTED_TEMPLATE_PARTIAL_SPEC_WKND + // Note: We can't nest the iterator inside the struct because we run into // a MSVC10 bug involving partial specializations of nested templates. @@ -219,21 +333,18 @@ typedef boost_fusion_detail_Seq sequence_type; \ \ BOOST_FUSION_ITERATOR_NAME(NAME)(boost_fusion_detail_Seq& seq) \ - : seq_(seq) {} \ + : seq_(seq) \ + BOOST_FUSION_DEFINE_ITERATOR_WKND_INIT_LIST_ENTRIES( \ + (0)ATTRIBUTES_SEQ) \ + {} \ \ boost_fusion_detail_Seq& seq_; \ \ - template struct value_of; \ - BOOST_PP_REPEAT( \ - ATTRIBUTES_SEQ_SIZE, \ - BOOST_FUSION_MAKE_ITERATOR_VALUE_OF_SPECS, \ - NAME) \ + BOOST_FUSION_DEFINE_ITERATOR_WKND_MEMBERS(ATTRIBUTES_SEQ_SIZE) \ \ - template struct deref; \ - BOOST_PP_SEQ_FOR_EACH_I( \ - BOOST_FUSION_MAKE_ITERATOR_DEREF_SPECS, \ - NAME, \ - ATTRIBUTES_SEQ) \ + BOOST_FUSION_DEFINE_ITERATOR_VALUE_OF(NAME, ATTRIBUTES_SEQ_SIZE) \ + \ + BOOST_FUSION_DEFINE_ITERATOR_DEREF(NAME, ATTRIBUTES_SEQ) \ \ template \ struct next \ diff --git a/test/sequence/define_struct_inline.cpp b/test/sequence/define_struct_inline.cpp index 9e3f6277..9cbd8ea0 100644 --- a/test/sequence/define_struct_inline.cpp +++ b/test/sequence/define_struct_inline.cpp @@ -26,6 +26,16 @@ struct cls ) }; +template +struct tpl_cls +{ + BOOST_FUSION_DEFINE_STRUCT_INLINE( + point, + (int, x) + (int, y) + ) +}; + namespace ns { BOOST_FUSION_DEFINE_STRUCT_INLINE(s, (int, m)) @@ -33,8 +43,8 @@ namespace ns BOOST_FUSION_DEFINE_STRUCT_INLINE(empty_struct, ) } -int -main() +template +void run_test() { using namespace boost::fusion; @@ -48,8 +58,8 @@ main() } { - BOOST_MPL_ASSERT_NOT((traits::is_view)); - cls::point p(123, 456); + BOOST_MPL_ASSERT_NOT((traits::is_view)); + Point p(123, 456); std::cout << at_c<0>(p) << std::endl; std::cout << at_c<1>(p) << std::endl; @@ -60,8 +70,8 @@ main() at_c<1>(p) = 9; BOOST_TEST(p == make_vector(6, 9)); - BOOST_STATIC_ASSERT(boost::fusion::result_of::size::value == 2); - BOOST_STATIC_ASSERT(!boost::fusion::result_of::empty::value); + BOOST_STATIC_ASSERT(boost::fusion::result_of::size::value == 2); + BOOST_STATIC_ASSERT(!boost::fusion::result_of::empty::value); BOOST_TEST(front(p) == 6); BOOST_TEST(back(p) == 9); @@ -69,7 +79,7 @@ main() { vector v1(4, 2); - cls::point v2(5, 3); + Point v2(5, 3); vector v3(5, 4); BOOST_TEST(v1 < v2); BOOST_TEST(v1 <= v2); @@ -82,15 +92,15 @@ main() } { - // conversion from cls::point to vector - cls::point p(5, 3); + // conversion from Point to vector + Point p(5, 3); vector v(p); v = p; } { - // conversion from cls::point to list - cls::point p(5, 3); + // conversion from Point to list + Point p(5, 3); list l(p); l = p; } @@ -105,13 +115,20 @@ main() } { - cls::point p = make_list(5,3); + Point p = make_list(5,3); BOOST_TEST(p == make_vector(5,3)); p = make_list(3,5); BOOST_TEST(p == make_vector(3,5)); } - - return boost::report_errors(); +} + +int +main() +{ + run_test(); // test with non-template enclosing class + run_test::point>(); // test with template enclosing class + return boost::report_errors(); + } diff --git a/test/sequence/define_tpl_struct_inline.cpp b/test/sequence/define_tpl_struct_inline.cpp index 1052f5db..fad064f7 100644 --- a/test/sequence/define_tpl_struct_inline.cpp +++ b/test/sequence/define_tpl_struct_inline.cpp @@ -27,6 +27,17 @@ struct cls ) }; +template +struct tpl_cls +{ + BOOST_FUSION_DEFINE_TPL_STRUCT_INLINE( + (X)(Y), + point, + (X, x) + (Y, y) + ) +}; + namespace ns { BOOST_FUSION_DEFINE_TPL_STRUCT_INLINE((M), s, (M, m)) @@ -34,13 +45,11 @@ namespace ns BOOST_FUSION_DEFINE_TPL_STRUCT_INLINE((M), empty_struct, ) } -int -main() +template +void run_test() { using namespace boost::fusion; - typedef cls::point point; - std::cout << tuple_open('['); std::cout << tuple_close(']'); std::cout << tuple_delimiter(", "); @@ -51,8 +60,8 @@ main() } { - BOOST_MPL_ASSERT_NOT((traits::is_view)); - point p(123, 456); + BOOST_MPL_ASSERT_NOT((traits::is_view)); + Point p(123, 456); std::cout << at_c<0>(p) << std::endl; std::cout << at_c<1>(p) << std::endl; @@ -63,8 +72,8 @@ main() at_c<1>(p) = 9; BOOST_TEST(p == make_vector(6, 9)); - BOOST_STATIC_ASSERT(boost::fusion::result_of::size::value == 2); - BOOST_STATIC_ASSERT(!boost::fusion::result_of::empty::value); + BOOST_STATIC_ASSERT(boost::fusion::result_of::size::value == 2); + BOOST_STATIC_ASSERT(!boost::fusion::result_of::empty::value); BOOST_TEST(front(p) == 6); BOOST_TEST(back(p) == 9); @@ -72,7 +81,7 @@ main() { vector v1(4, 2); - point v2(5, 3); + Point v2(5, 3); vector v3(5, 4); BOOST_TEST(v1 < v2); BOOST_TEST(v1 <= v2); @@ -85,15 +94,15 @@ main() } { - // conversion from point to vector - point p(5, 3); + // conversion from Point to vector + Point p(5, 3); vector v(p); v = p; } { - // conversion from point to list - point p(5, 3); + // conversion from Point to list + Point p(5, 3); list l(p); l = p; } @@ -109,13 +118,20 @@ main() { - point p = make_list(5,3); + Point p = make_list(5,3); BOOST_TEST(p == make_vector(5,3)); p = make_list(3,5); BOOST_TEST(p == make_vector(3,5)); } +} +int +main() +{ + run_test >(); // test non-template enclosing class + run_test::point >(); // test template enclosing class + return boost::report_errors(); }