Question: ```
//a class template to do something to STL container
template<typename T/*should be a STL container*/,typename Ele/*the type of element*/>
struct foo{
T a_container_with_an_element(){
    T con;
    Ele e;
    con.push_back(++++e);
    return con;
}

```

As you can see, it's really foolish to put the element's type into templates' parameter because it's already contained in the container's type.

So, is there any magic to get the element's type out of T?

Lots of Thx :-)

Answer: 
If the container is a standard library container, the name of the element is an embedded name of the container as follows:

```
typedef typename T::value_type type;

```

The standard container's have a few standard names in them (for example, see [vector on cppreference](http://en.cppreference.com/w/cpp/container/vector)) and the [C++ standard](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3797.pdf) §23.2.

```
X::value_type
X::reference
X::const_reference
X::iterator
X::const_iterator
X::difference_type
X::size_type

```
If `T` should be an STL container you could use a template-template parameter. It must be a container that supports `push_back()` and we will incorporate the fact that STL containers contain a 2nd "allocator" parameter.

In that case you can use

```
template< template<typename, typename> class Con,  
          typename Ele, typename Alloc = std::allocator<Ele> >
struct foo
{
  typedef Con<Ele, Alloc> container_type;
  typedef Ele value_type;

  container_type a_container_with_an_element()
  {
    container_type con;
    value_type e;
    con.push_back( e ); // or modify e first but not ++++e
    return con;
  }
};

```

Your code would then do

```
foo< std::vector, int > myfoo;
std::vector<int> myvec = myfoo.a_container_with_an_element();

```

If you want just one template parameter

```
template< typename Con >
struct foo
{
  typedef typename Con::value_type value_type;
  Con a_container_with_an_element()
  {
    Con con;
    value_type e;
    con.push_back( e ); // or modify e first but not ++++e
    return con;
  }
};

```

and usage:

```
 foo<std::vector<int> > myfoo;
 std::vector<int> myvec = myfoo.a_container_with_an_element();

```

Note that in this example you can also do `foo<std::vector<int> >::Ele` to get the element type. You could make a `typedef` for the container type too. Often used in templates. (You need `typename` when the template is not fully qualified. So `foo<std::vector<int>>` is fully qualified and doesn't need it there.