Try std::type_index with C++Builder[JAPAN]

Posted by on in Programming

There is "std::type_index" as a new function of C++11.

I tried using that function.

"std::type_index"  case a useful feature to be stored in the container.It can store type information.

I tried "std::type_index" with MPL. boost 1.55 is included with C++builder 10.1 Berlin.

I used boost::mpl::vector<>.This MPL is the meaning of "C++ template metaprogramming".

http://www.boost.org/doc/libs/1_55_0/libs/mpl/doc/refmanual.html

////
#include <boost/mpl/vector.hpp>
#include <boost/mpl/at.hpp>
#include <boost/mpl/if.hpp>
#include <boost/mpl/vector_c.hpp>
#include <boost/version.hpp>

#include <typeindex>
#include <map>
////
void __fastcall TForm1::FormCreate(TObject *Sender)
{
	namespace  mpl = boost::mpl;
	using Tvc1 = mpl::vector_c<int, 0,1,2, BOOST_VERSION >;
	using Tv1 = mpl::vector3<TRectangle, TButton, TLabel>;

	std::map<std::type_index, String> map1;
	map1.insert(
		std::make_pair<std::type_index, String>(
		typeid(mpl::at_c<Tv1, mpl::at_c<Tvc1, 0>::type::value >::type  *),
		Format("typeid = %s", ARRAYOFCONST((typeid(mpl::at_c<Tv1, 0>::type  *).name() )) ))
		);

	mpl::at_c<Tv1, mpl::at_c<Tvc1, 0>::type::value >::type* Rectangle1
		= new mpl::at_c<Tv1, mpl::at_c<Tvc1, 0>::type::value >::type(this);
	mpl::at_c<Tv1, mpl::at_c<Tvc1, 1>::type::value >::type* Button2
		= new mpl::at_c<Tv1, mpl::at_c<Tvc1, 1>::type::value >::type(this);
	mpl::at_c<Tv1, mpl::at_c<Tvc1, 2>::type::value >::type* Label3
		= new mpl::at_c<Tv1, mpl::at_c<Tvc1, 2>::type::value >::type(this);
	this->AddObject(Rectangle1);
	this->AddObject(Button2);
	this->AddObject(Label3);

	ShowMessage( map1[std::type_index(typeid(Rectangle1))] );
}
//---------------------------------------------------------------------------

std::map<std::type_index, String> was used.

Returns the mapped String if you type. To MPL of the container, it has put the type. 

////
mpl::at_c<Tv1, mpl::at_c<Tvc1, 0>::type::value >::type

It can call one type. 

Finally we take String out of the std::map.

////
map1[std::type_index(typeid(Rectangle1))];

 

 


 



About
Gold User, No rank,
Delphi / C++Builder blogger

Comments

Check out more tips and tricks in this development video: