Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added demo for function that returns a run-time variant-type list; re…

…named that function; made sure implementation worked.
  • Loading branch information...
commit c22c478282cc10a647942bd49e27f866c1926b72 1 parent d47a21a
@CTMacUser authored
View
8 boost/unions/super_union.hpp
@@ -95,8 +95,8 @@ template < >
union super_union<>
{
//! Returns a list of the union's variant members' types.
- static auto optioned_types() -> std::array<std::type_info const *, 0>
- { return {}; }
+ static auto variant_types() -> std::array<std::type_info const *, 0>
+ { return { {} }; }
};
//! Specialization of `super_union` when the type list is not empty
@@ -117,9 +117,9 @@ union super_union<Head, Tail...>
{
//! Returns a list of the union's variant members' types.
static
- auto optioned_types()
+ auto variant_types()
-> std::array<std::type_info const *, 1 + sizeof...(Tail)>
- { return { &typeid(Head), &typeid(Tail)... }; }
+ { return { {&typeid(Head), &typeid(Tail)...} }; }
Head data;
super_union<Tail...> rest;
View
32 libs/unions/example/super_union_basics.cpp
@@ -8,8 +8,10 @@
#include "boost/unions/super_union.hpp" // for boost::unions::super_union
+#include <array> // for std::array
#include <iostream> // for std::cout
-#include <ostream> // for std::endl
+#include <ostream> // for std::basic_ostream, endl
+#include <typeinfo> // for std::type_info
// The union we'll we working with...
@@ -37,6 +39,27 @@ intdouble_t make_double()
return result;
}
+// Reporters
+template < typename Ch, class Tr, typename InIter >
+std::basic_ostream<Ch, Tr> &
+print_type_list( std::basic_ostream<Ch, Tr> &o, InIter b, InIter e )
+{
+ o << "Listed types: ";
+ if ( b == e )
+ {
+ o << "(None)";
+ }
+ else
+ {
+ o << (**b).name();
+ while ( ++b != e )
+ {
+ o << ", " << (*b)->name();
+ }
+ }
+ return o << std::endl;
+}
+
// Main program
int main()
@@ -65,5 +88,12 @@ int main()
cout << "Print immediate through index: " << get<1>(make_double()) << '\n';
cout << "And another time: " << get<0>(make_int()) << endl;
+ boost::unions::super_union<> test3{};
+ auto const types1 = intdouble_t::variant_types();
+ auto const types2 = decltype(test3)::variant_types();
+
+ print_type_list( cout, types1.begin(), types1.end() );
+ print_type_list( cout, types2.begin(), types2.end() );
+
return 0;
}
Please sign in to comment.
Something went wrong with that request. Please try again.