0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

STLコンテナのインスタンスから要素の型を取得する

Last updated at Posted at 2015-06-08

こんなコードを書く必要性に迫られた。

template<class T>
struct foo{
 T operator()( ... ){ ... };
};
 
std::array< double, ... > a = {{ ... }};
bar( foo<  >() ); // <  >の中に a の要素の型 double を入れたい

別に手打ちでdoubleと書けば済む話なのだが、何とか型をaから引っ張り出したい。

そこで調べてみたら、stack overflow に答えがあった。
http://stackoverflow.com/questions/12391814/how-to-get-element-type-from-stl-container-instance (英語)

次のようにすれば良いようだ。

bar( foo< decltype(a)::value_type >() );
// decltype(a)::value_type は double

結構使える機会はあると思うのだが、あまりヒットしなかった。
ひょっとしてこんなことをしなければならないそもそもの設計が間違いなのだろうか。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?