Connecting Tech Pros Worldwide Forums | Help | Site Map

Please share your thoughts on Member Function Templates

Rajan
Guest
 
Posts: n/a
#1: Jul 23 '05
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


Victor Bazarov
Guest
 
Posts: n/a
#2: Jul 23 '05

re: Please share your thoughts on Member Function Templates


Rajan wrote:[color=blue]
> Can anybody share some of your thoughts in Member Function Templates
> implementation.Can you also give some example on it[/color]

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
Rajan
Guest
 
Posts: n/a
#3: Jul 23 '05

re: Please share your thoughts on Member Function Templates


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

Victor Bazarov
Guest
 
Posts: n/a
#4: Jul 23 '05

re: Please share your thoughts on Member Function Templates


Rajan wrote:[color=blue]
> 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.[/color]

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
red floyd
Guest
 
Posts: n/a
#5: Jul 23 '05

re: Please share your thoughts on Member Function Templates


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.
Closed Thread