Re: C4239 - why here?
- From: "Tom Widmer [VC++ MVP]" <tom_usenet@xxxxxxxxxxx>
- Date: Fri, 23 Sep 2005 16:41:08 +0100
Goran Pusic wrote:
void f(int& i) { i = 10; }
double d = 0; f(d); //would you expect d to be 10?
Yes, I see. In fact, i would expect the compiler to refuse this outright, it just makes no sense at all! Using intrinsic type promotions on references like this, tsk, tsk, naughty compiler... Pascal doesn't allow this! Does C#? I hope not :-))
Well, VC++ does at least give a diagnostic to indicate that the code has an error - the code is "ill-formed" C++, and VC++ is reporting that fact.
But, that's not my situation. I have X as a "polymorphic worker class", and
it may or not change inside X, I don't care. I just want to it passed to F
to get polymorphic behaviour in F depending on the calling context).
Sort-of:
BaseX
{ virtual f() }
X1:BaseX { overridden virtual F() }
X2, X3...
and then F(BaseX&) gets called like this: F(X1(params)), F(X2(params)),
F(X3(params)) etc...
Perhaps F should take a const reference and f() should be a const member? Alternatively, there is this workaround that must be used with care:
template <class T>
inline T& ref_from_temp(T const& t)
{
return const_cast<T&>(t);
}F(ref_from_temp(X1(params)));
Tom .
- References:
- C4239 - why here?
- From: Goran Pusic
- Re: C4239 - why here?
- From: Tom Widmer [VC++ MVP]
- Re: C4239 - why here?
- From: Goran Pusic
- C4239 - why here?
- Prev by Date: Re: C4239 - why here?
- Next by Date: is the best way to wrap C functions with mananged C++ or C#?
- Previous by thread: Re: C4239 - why here?
- Next by thread: is the best way to wrap C functions with mananged C++ or C#?
- Index(es):
Relevant Pages
|