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: 
In your special case where you know that T is a STL container Naill's answer is correct, use the nested types in the container.

If however you want a more generic "I know I am getting a template as T and I want the type of the first parameter of that template" case you can get the type through template specialization:

```
template<typename T>
struct MyFirstElement;
template<template<typename...> class T_Container, typename T_Type, typename... Ts>
struct MyFirstElement<T_Container<T_Type,Ts...>>{
    using Type = T_Type;
};

//unit test
using MustBeInt = typename MyFirstElement<std::vector<int>>::Type;
using MustBeLong = typename MyFirstElement<std::map<long,float>>::Type;

static_assert(std::is_same<MustBeInt,int>::value,"");
static_assert(std::is_same<MustBeLong,long>::value,""); 

//this works too even though its not an STL container
template<typename T, typename U, typename Z>
struct MyCrazyContainer{};

static_assert(std::is_same<typename MyFirstElement<MyCrazyContainer<bool,long,float>>::Type,bool>::value,"");

```

here is a live example: <http://ideone.com/OxhSOc>

As Jarod42 pointed out this will only work with templates which are "regular" i.e. only have types as parameters. std::array for example breatks this because it has an int as a second parameter.
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.