Please share your thoughts on Member Function Templates

R

Rajan

Hi All C++ Experts
Can anybody share some of your thoughts in Member Function Templates
implementation.Can you also give some example on it

Best Regards
Raj
 
V

Victor Bazarov

Rajan said:
Can anybody share some of your thoughts in Member Function Templates
implementation.Can you also give some example on it

What exactly would you like to know? Here is an example:

class ConvertibleToAPointer {
// ...
public:
template<class T> operator T*();
};

Here is another example:

class CanWorkOnAPointer {
// ...
public:
template<class T> void workOn(T*);
};

As to thoughts, here is one: unfortunately some compilers don't support
those well, as I discovered today. But then again, they just have some
bad template support in general...

V
 
R

Rajan

Hi
Victor you are correct , i was having code like this
class ConvertibleToAPointer {
// ...
public:
template<class T> operator T*();
};


void fun(char *x)
{}

main()
{
ConvertibleToAPointer obj;
fun(obj);

}

so this code is not compiling . i think my compiler is not supporting
member template function.

Is there any get around way of doing this.

regards rajendra
 
V

Victor Bazarov

Rajan said:
Victor you are correct , i was having code like this
class ConvertibleToAPointer {
// ...
public:
template<class T> operator T*();
};


void fun(char *x)
{}

main()
{
ConvertibleToAPointer obj;
fun(obj);

}

so this code is not compiling . i think my compiler is not supporting
member template function.

Is there any get around way of doing this.

Try to specify the type explicitly:

class ConvertibleToAPointer {
public:
template<class T> T* convert2ptr() { return 0; }
};

void fun(char* x) {}

int main()
{
ConvertibleToAPointer obj;
fun(obj.convert2ptr<char>());
}

If your compiler has other quirks, you might consider posting questions
about them to that compiler's newsgroup (if it exists, of course).

V
 
R

red floyd

Victor, Rajan is using VC6.

Rajan, As I said earlier, VC6 is known to have issues with template
code, especially member function templates. You need to upgrade your
compiler.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top