Re: C4239 - why here?



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
.



Relevant Pages

  • Re: C4239 - why here?
    ... > void f ... In fact, i would expect the compiler to refuse this outright, it ... like this, tsk, tsk, naughty compiler... ...
    (microsoft.public.dotnet.languages.vc)
  • Re: Microfocus COBOL 3.2.43 (16bit)
    ... Tsk tsk... ... dual processor IBM LS20 blade with dual fibre connections & eight gigs of ... NT Software, Compiler, etc. ... you could host it on a Windows Server or Windows Vista machine ...
    (comp.lang.cobol)
  • Re: Benefit of not defining the order of execution
    ... the contents of the log can vary depending on the evaluation order, ... execution is that the course of execution is not well defined. ... It can vary from environment to environment, from compiler to ... we need an additional suite of coding standards. ...
    (comp.lang.c)
  • Re: In-Out Parameters for functions
    ... > Why should buggy code work? ... As Ada stands now, it is perfectly ... the compiler cannot know whether particular ... then depending on the order of evaluation within an ...
    (comp.lang.ada)
  • Re: Request for testing of Reltive File status
    ... IBM ILE COBOL (or whatever the iSeries compiler is called now) ... Depending on your environment, you will probably have to modify the "ASSIGN ... Select RelFile Assign RelName ... Relative Key RelKey. ...
    (comp.lang.cobol)