help-gplusplus
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Is it valid to call 'operator T()' directly in an template function?


From: fik
Subject: Re: Is it valid to call 'operator T()' directly in an template function?
Date: Thu, 18 Nov 2004 13:42:46 +1100

On further thoughts, the code as is, shouldn't run because value in Field is 
never instantiated. I wonder whether putting in a constructor/destructor for 
Field will help? Perhaps the compiler realizes that value is never 
instantiated in the class and optimises it away along with the operators.

Try the following constructor/destructor in Field:
Field ( void ) : value(0) { value = new void *; }

~Field ( void ) { if ( value ) delete value; }






"Feng Ye" <fengye@hotpop.com> wrote in message 
news:mailman.3236.1100678397.8225.help-gplusplus@gnu.org...
> Hi,
>   I got this error when compiling the following code by g++ 3.4.3.
>
>   error: 'const class Field' has no member named 'operator T'
>
>   The comeau online c++ reports no error. Is it a bug of g++?
>
> Regards,
> Feng Ye
>
> =====================8<===========================
> class Field
> {
> public:
>     operator int() const { return *(int*)value; }
>     operator char*() const { return (char*)value; }
>
> private:
>     void* value;
> };
>
> template<class T>
> class Test
> {
> public:
>     void setValue(int){}
>     void setValue(char*){}
>
>     void fromField(const Field& f);
> };
>
> template<class T>
> void Test<T>::fromField(const Field& f)
> {
>     setValue( f.operator T() );
> }
>
> int main()
> {
>     Field f;
>     Test<int> t;
>
>     t.fromField(f);
> }
>
>
> 




reply via email to

[Prev in Thread] Current Thread [Next in Thread]