1

while using template <typename p> in my program its not giving any compilation error but i use template <class p> is giving error while i am passing two vector of different types.

template <class p>

getvector(std::vector<p>&vec)
{
// my operation
}

this is the function which will receive the vector , i am using the template <typename p> it not giving compilation error. can any body explain what makes if differ from template <typename p >

sss
  • 21
  • 3
  • 2
    In this scenario, there is no difference between `typename` and `class`. Your code should pass the compilation. You can provide more detail of the error message. – llllllllll Jan 23 '18 at 09:10
  • In both cases function needs to have return type specified. Try `template void getvector(...) { ... }` – Daniel Langr Jan 23 '18 at 09:10

1 Answers1

1

There is no difference between the typename and a class in function template declaration. When declaring a template parameter they mean the same thing. Your function needs a return type:

template <class p>
std::vector<p> getvector(std::vector<p> &vec)
{
    // your code
    return vec;
}

Which is the same as:

template <typename p>
std::vector<p> getvector(std::vector<p> &vec)
{
    // your code
    return vec;
}

If you don't want to return a simple void function would be:

template <typename p> // or template <class p>
void getvector(std::vector<p> &vec)
{
    std::cout << vec[0]; // your code here
}
Ron
  • 14,674
  • 4
  • 34
  • 47