Watch, Follow, &
Connect with Us

Please visit our new home
community.embarcadero.com.


Welcome, Guest
Guest Settings
Help

Thread: XE8 enterprice, bcc32 : warning 8006 - Initialising X with Y



Permlink Replies: 4 - Last Post: Jan 10, 2017 11:55 PM Last Post By: Jan Dijkstra Threads: [ Previous | Next ]
Jan Dijkstra

Posts: 206
Registered: 11/4/99
XE8 enterprice, bcc32 : warning 8006 - Initialising X with Y
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 9, 2017 3:28 AM
I have done the following:
struct PACKAGE SColorRec : public TColorRec
{
private:
  bool __fastcall GetBool (int index) const;
  int  __fastcall GetInt (int index) const;
  void __fastcall SetInt (int index, int value);
 
public:
       __fastcall SColorRec (TColor color);
       __fastcall SColorRec (int red, int green, int blue);
       __fastcall SColorRec (void);
  void __fastcall SetDefault (void);
 
  SColorRec & __fastcall operator= (TColor color);
  SColorRec & __fastcall operator= (int iColor);
  bool        __fastcall operator== (TColor color) const;
  bool        __fastcall operator!= (TColor color) const;
              __fastcall operator TColor (void) const;
 
  __property bool isDefault = { read = GetBool, index = 0 };
  __property bool isSystem  = { read = GetBool, index = 1 };
  __property int  red       = { read = GetInt, write = SetInt, index = 0 };
  __property int  green     = { read = GetInt, write = SetInt, index = 1 };
  __property int  blue      = { read = GetInt, write = SetInt, index = 2 };
  __property int  iColor    = { read = GetInt, write = SetInt, index = 3 };
};

Basically enhancing TColorRec with constructors and some range checking on values. I've added the TColor cast operator to be able to use an SColorRec there where a TColor is expected. I've done this sort of thing before in other classes, and it normally works like a charm.

But in this particular case, the cast operator seems to confuse the compiler.

When I have an instance of an SColorRec somewhere, and pass it to a routine that expects a TColor, I get the following warning
[bcc32 Warning] bfw3dbars.cpp(376): W8006 Initializing TColor with SColorRec

However, when I inspect the code that has been generated, the compiler did put in a call to my cast operator.

So the question remains is this: why did the compiler, for this particular class and cast operator, issue the warning as if I was passing the structure itself around? In other cases where cast operators are invoked this warning is not issued.
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: XE8 enterprice, bcc32 : warning 8006 - Initialising X with Y
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 9, 2017 9:41 AM   in response to: Jan Dijkstra in response to: Jan Dijkstra
Jan wrote:

When I have an instance of an SColorRec somewhere, and pass it
to a routine that expects a TColor, I get the following warning

 
[bcc32 Warning] bfw3dbars.cpp(376): W8006 Initializing TColor with
SColorRec
 

Please show your actual code that is generating that warning. Is the function
parameter expecting the TColor to be passed by value or by reference?

--
Remy Lebeau (TeamB)
Jan Dijkstra

Posts: 206
Registered: 11/4/99
Re: XE8 enterprice, bcc32 : warning 8006 - Initialising X with Y
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 10, 2017 12:08 AM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Remy Lebeau (TeamB) wrote:
Jan wrote:

When I have an instance of an SColorRec somewhere, and pass it
to a routine that expects a TColor, I get the following warning

 
[bcc32 Warning] bfw3dbars.cpp(376): W8006 Initializing TColor with
SColorRec
 

Please show your actual code that is generating that warning. Is the function
parameter expecting the TColor to be passed by value or by reference?

--
Remy Lebeau (TeamB)

Excerpt from the class it's used in
class PACKAGE TBfwBarItem : public TBfwPart3dCoreIdItem
{
private:
 
  SColorRec   FBarColor;        // A specific color in which the bar should be rendered
 
  TColor                       __fastcall GetColor (void);
 
public:
               __fastcall  TBfwBarItem (void);
               __fastcall  TBfwBarItem (__int64 key);
};
 
TColor __fastcall TBfwBarItem::GetColor (void)
{
// The compiler issues a warning, despite putting, correctly, a call to the
// TColor cast operator. So we suppress this warning.
#pragma option push -w-8006
  return FBarColor;
#pragma option  pop
}

If I remove the pragma's, I get the warning on the line with return FBarColor;

The code that is generated is calling the cast operator though, and thus is returning exactly what is required.

Trying to outsmart the compiler by
static TColor __fastcall getColor (TColor color)
{
  return color;
}
 
TColor __fastcall TBfwBarItem::GetColor void)
{
  return getColor (FBarColor);
}

still generates the warning on return getColor (FBarColor), despite (again) the correct cast operator being called.

The funny bit is, that when I add
void __fastcall TBfwBarItem::SetColor (TColor color)
{
  if (color != FBarColor)
  {
    Changing ();
    FBarColor = color;
    Changed ();
  }
}

then I get no warning on the line with the 'if'. There, too, the cast operator is called.
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: XE8 enterprice, bcc32 : warning 8006 - Initialising X with Y [Edit]
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 10, 2017 10:08 AM   in response to: Jan Dijkstra in response to: Jan Dijkstra
Jan wrote:

Excerpt from the class it's used in

You said earlier that the problem was related to passing an SColorRec to
a routine, not as a return value. You are likely being bit by (N)RVO:

https://en.wikipedia.org/wiki/Return_value_optimization

#pragma option push -w-8006
return FBarColor;
#pragma option pop

Rather than use #pragma to disable the warning, am explicit type-cast should
prevent the warning from being generated in the first place:

return (TColor) FBarColor;


Or better:

return static_cast<TColor>(FBarColor);


--
Remy Lebeau (TeamB)
Jan Dijkstra

Posts: 206
Registered: 11/4/99
Re: XE8 enterprice, bcc32 : warning 8006 - Initialising X with Y [Edit]
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 10, 2017 11:55 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Remy Lebeau (TeamB) wrote:
Jan wrote:

Excerpt from the class it's used in

You said earlier that the problem was related to passing an SColorRec to
a routine, not as a return value. You are likely being bit by (N)RVO:

Yes. That was because I had tried the bypass with the static getColor, and the initial comment was based on that. Pass an SColorRec to the static, where the parameter type is defined as a TColor.

https://en.wikipedia.org/wiki/Return_value_optimization

#pragma option push -w-8006
return FBarColor;
#pragma option pop

Rather than use #pragma to disable the warning, am explicit type-cast should
prevent the warning from being generated in the first place:

Yes, that could work too. But that still does not explain why the warning is generated in the first place. The compiler generates the call to the cast operator in all the cases I have shown, but only in the case where it's used in an if condition, it does not generate the warning. I'm just trying to figure out what the difference is.

return (TColor) FBarColor;


Or better:

return static_cast<TColor>(FBarColor);


--
Remy Lebeau (TeamB)
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02