[cfe-dev] Typeid and forward declaration - Clarification needed

Karen Shaeffer shaeffer at neuralscape.com
Tue Oct 29 15:00:41 PDT 2013


On Tue, Oct 29, 2013 at 02:49:58PM -0700, Richard Smith wrote:
> On Tue, Oct 29, 2013 at 2:40 PM, Karen Shaeffer <shaeffer at neuralscape.com>wrote:
> 
> > On Tue, Oct 29, 2013 at 02:14:11PM -0700, Richard Smith wrote:
> > > On Tue, Oct 29, 2013 at 1:29 AM, Rahul Jain <1989.rahuljain at gmail.com
> > >wrote:
> > >
> > > >
> > > > Hi all,
> > > >
> > > > This is with respect to the following TC.
> > > >
> > > > struct S;
> > > >
> > > > template <typename T>
> > > > static bool fn (S *s)
> > > > {
> > > >   return typeid (*s) == typeid (T);
> > > > }
> > > >
> > > > struct S
> > > > {
> > > > };
> > > >
> > > > bool x = fn<S> (__null);
> > > >
> > > >
> > > > The standard says:
> > > > If the type of the type-id is a class type or a reference to a class
> > type,
> > > > the class shall be completely-defined.
> > > >
> > > >
> > > > This "shall" is kind of confusing. Is this a valid TC. Is forward
> > > > declaration allowed while using the typeid operator?
> > > >
> > >
> > > No, but the rules are slightly different because this occurs inside a
> > > template. This testcase is ill-formed, but no diagnostic is required, per
> > > [temp.res](14.6)p8:
> > >
> > > "If a type used in a non-dependent name is incomplete at the point at
> > which
> > > a template is defined but is complete at the point at which an
> > > instantiation is done, and if the completeness of that type affects
> > whether
> > > or not the program is well-formed or affects the semantics of the
> > program,
> > > the program is ill-formed; no diagnostic is required."
> > >
> > > So the template has no valid specializations. Further, we're allowed to
> > > reject this even if the template is never instantiated:
> > >
> > > "If no valid specialization can be generated for a template, and that
> > > template is not instantiated, the template is ill-formed, no diagnostic
> > > required."
> > >
> >
> > Hi Richard,
> > There is no ambiguity there. The standard defines it concisely. I actually
> > thought
> > whether a compiler rejected that example was implementation specific,
> > depending on
> > the compiler's template instantiation stategy. But c++11 defines it as an
> > ill-formed
> > program. Is that test case an ill-formed program in the previous standard?
> >
> 
> The wording was added by DR206 in response to the ambiguity in the original
> (C++98) spec. Since this was a DR, it retroactively applies to C++98 too,
> so this code has (in some sense) always been ill-formed, with no diagnostic
> required:
> 
> http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#206
> 
> I'm not sure what you're talking about when you mention ambiguity.

Hi Richard,
I simply meant the standard is clear and concise as you point out. And this
issue has nothing to do with the discretion of the implementation. It is an
ill-formed program. Thank you very much for clarifying the details. Very helpful.

enjoy,
Karen
-- 
Karen Shaeffer                 Be aware: If you see an obstacle in your path,
Neuralscape Services           that obstacle is your path.        Zen proverb



More information about the cfe-dev mailing list