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: Lionel B
Subject: Re: Is it valid to call 'operator T()' directly in an template function?
Date: 18 Nov 2004 03:40:42 -0800
User-agent: G2/0.2

fik wrote:
> On further thoughts, the code as is, shouldn't run because
> value in Field is never instantiated.

I'm not sure "instantiated" is the right word:

Field f;

creates a data member f.value - a pointer of type void* which doesn't
point at anything in particular.

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

Well, as far as I can make out the code is syntactically correct, if
semantically daft [since SetValue does nothing - and even if it did
something, the call to f.operator T() would result in Undefined
Behaviour, due to dereference of the un-initialised pointer f.value].

But if the code is syntactically correct, then regardless of
optimisation, etc. the compiler should not report an error. If the
compiler detects something dumb (as well it might in this case) it
might issue a *warning*, but not an error.

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

<???> new void* returns an object of type void** ... which is then cast
to the type of value, which is void* </???>  ... nonsense

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

1) C++ doesn't use void as a parameter type.

2) If the (silly) call

value = new void *;

failed, you would never hit the destructor since a std::bad_alloc
exception would be thrown.

P.S. Please don't top-post.

-- 
Lionel B



reply via email to

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