This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c++/14263] [3.3/3.4/3.5 Regression] Cryptic diagnostic for cast from virtual base class not using dynamic_cast


------- Additional Comments From gdr at integrable-solutions dot net  2004-03-01 02:45 -------
Subject: Re:  [3.3/3.4/3.5 Regression] Cryptic diagnostic for cast from virtual base class not using dynamic_cast

"mmitchel at gcc dot gnu dot org" <gcc-bugzilla@gcc.gnu.org> writes:

| The new diagnostic is arguably better than the old: it gives you the
| hint that the problem is that A is virtual.  Talking about
| static_cast would be fine, but it's easy to see that the static_cast
| is causing the conversion. 

Not really, in real-world.  I did not invent the testcase (the only
thing I did was to reduce it to minimum).  When presented to the real
problem, from real codes, I had to pause a while before realizing what
was going one.  Yet, I consider the people that come to that problem
not to be freshmen in C++.  The people knew that using a "C"-style
conversion would probably "appear" to work but, it would most probably
do something else.  They knew that B directly derive from A (which in
part makes them think they could legitimately use static_cast instead
of dynamic_cast); they could not understand the wording of the message. 

| The biggest problem here is that "via virtual base A" is silly when
| "A" is a virtual base -- 

That added a lot to the confusion.


| a better message would be:
| 
|   cannot convert from `A*' to `B*' because `A' is virtual base of `B'

That is an improvement over existing wording but still it does not
address the point of the PR. 

| Talking about dynamic_cast is confusing because that will lead people to use
| dynamic_cast even in situations where its use is not appropriate.  It may be

I strongly disagree.  When you have a virtual base class, the only way
you can legitimately cast back is by using dynamic_cast<> and nothing
else.  The keywords here are "virtual base class" vs. "static_cast<>".

When you refuse to tell people the only safe alternative (compared to
C-style cast), you run a *higher* risk to having them make the wrong
choice.  

| that the user meant to cast "A*" to a base of "A", but instead used a class
| derived from "A".

No, when you have a virtual base class, you have no other choice.
There is nothing wrong suggesting the rigth to do -- even if they may
shoot themselves in a completely different situations that is not
connected.  The important thing is that they get a better diagnostic
in the appropriate contexts.

Please, reconsider the testcase.

| I would disapprove a patch to change the wording to match the
| suggested wording in Comment #1.

I believe that position is missing the whole point of the PR.

-- Gaby


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=14263


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]