Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

class template vs. member template

Tags:

c++

templates

Is there a good rule when to use a class template instead of using member templates? As I understand it, your are forced to use a class template if your class contains member variable templates but otherwise you are free of choice since this:

template<typename T>
class Foo
{
public:
    void someMethod(T x);
};

behaves like this:

class Foo
{
public:
    template<typename T>
    void someMethod(T x);
};

Is my claim wrong? Or is there any rule of thumb when to use what?

like image 472
ZweeBugg Avatar asked Sep 14 '25 08:09

ZweeBugg


1 Answers

The two are not at all the same. With the first:

Foo<int> f;
f.someMethod('a');

the called function is someMethod(int).

With the second:

Foo f;
f.someMethod('a');

the called function is someMethod(char).

like image 162
Pete Becker Avatar answered Sep 16 '25 20:09

Pete Becker