gcc4.5で、初期化リストを試すのを忘れていた。
// for name demangling.
class Demangle
{
private :
char * realname ;
public :
Demangle( std::type_info const & ti )
{
int status = 0 ;
realname = abi::__cxa_demangle( ti.name(), 0, 0, &status ) ;
}
Demangle( Demangle const & ) = delete ;
Demangle & operator = ( Demangle const & ) = delete ;
~Demangle()
{
std::free( realname ) ;
}
operator char const * () const
{
return realname ;
}
} ;
// ここからコード。
template < typename Iterator >
void print( Iterator first, Iterator last)
{
typedef typename std::iterator_traits<Iterator>::value_type type ;
std::for_each( first, last,
[](type const & value)
{
std::cout << value << std::endl ;
}
) ;
}
template < typename T >
void f( std::initializer_list<T> list )
{
std::cout << "type: " << Demangle( typeid(T) ) << std::endl ;
print( list.begin(), list.end() ) ;
}
int main()
{
f({1, 132131, 1323, -3231, 12331, 3422, 875, -5245435, 0, 44, 333}) ;
f({1.0, 0.0, -999.32113, 3.141592, 99999999999999.0, 0.000000001}) ;
f({"hello", "The World", "WRRRRRRRRRY!"}) ;
f< std::string >({"Annie", "Are You OK?", "So, Annie Are You OK", "Are You Ok, Annie?"}) ;
std::vector<int> v({0, 1, 2, 3, 4}) ;
print( v.begin(), v.end() ) ;
}
普通に動きすぎて、何の面白味もない機能だ。まあ、動くということは、実に素晴らしいことだ。実際、初期化リストは、それほど面白い機能ではない。ただ、動くために作られた機能だ。
No comments:
Post a Comment
You can use some HTML elements, such as <b>, <i>, <a>, also, some characters need to be entity referenced such as <, > and & Your comment may need to be confirmed by blog author. Your comment will be published under GFDL 1.3 or later license with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.