[PATCH] D11194: Instantiate function declarations in instantiated functions.

Richard Smith via cfe-commits cfe-commits at lists.llvm.org
Tue Aug 18 18:25:07 PDT 2015


On Tue, Aug 18, 2015 at 10:05 AM, Serge Pavlov <sepavloff at gmail.com> wrote:

> sepavloff added inline comments.
>
> ================
> Comment at: lib/AST/DeclBase.cpp:273
> @@ +272,3 @@
> +    return true;
> +  if (const CXXRecordDecl *ClassD = dyn_cast<CXXRecordDecl>(LDC))
> +    return ClassD->isLocalClass() && !ClassD->isLambda();;
> ----------------
> rsmith wrote:
> > It's not necessary for this change, but to match its documentation this
> function should handle other kinds of `TagDecl` too (enums, C structs).
> Something like:
> >
> >   do {
> >     if (LDC->isFunctionOrMethod())
> >       return true;
> >     if (!isa<TagDecl>(LDC))
> >       return false;
> >     LDC = LDC->getLexicalParent();
> >   } while (LDC);
> >   return false;
> >
> > ... maybe?
> The proposed code recognizes lambda as lexically contained within a
> function. As a result, the following test starts to fail (obtained from
> CXX\expr\expr.prim\expr.prim.lambda\default-arguments.cpp):
> ```
> struct NoDefaultCtor {
>   NoDefaultCtor(const NoDefaultCtor&); // expected-note{{candidate
> constructor}}
>   ~NoDefaultCtor();
> };
>
> template<typename T>
> void defargs_in_template_unused(T t) {
>   auto l1 = [](const T& value = T()) { };
>   l1(t);
> }
>
> template void defargs_in_template_unused(NoDefaultCtor);
> ```
> because default value for lambda argument cannot be instantiated. It is
> not clear whether instantiation of the lambda default argument must always
> occur similar to DR1484. I couldn't find appropriate place in the standard.
> According to spirit it shouldn't as lambda is not a separate declaration
> but a part of instantiated content.


I agree. The default argument must be instantiated as part of instantiating
the surrounding function.


> If so  14.6.4.1p2 is more likely to be applied and the above test must
> pass.
>

The call operator of the lambda is neither a function template nor a member
function of a class template. The relevant rule is 14.7.1/1: "Within a
template declaration, a local class or enumeration and the members
of a local class are never considered to be entities that can be separately
instantiated (this includes their
default arguments, exception-specifications, and non-static data member
initializers, if any)." The lambda expression defines a local class, so its
members' default arguments are instantiated with the surrounding function.


> Maybe we need to rename `isLexicallyWithinFunctionOrMethod` to
> `shouldBeAlwaysInstantiated` or something like that to avoid
> misunderstanding?
>
>
>
> http://reviews.llvm.org/D11194
>
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20150818/5c162c6d/attachment.html>


More information about the cfe-commits mailing list