This is an archive of the discontinued LLVM Phabricator instance.

Sema: Do not allow template declarations inside local classes
ClosedPublic

Authored by majnemer on Oct 9 2013, 12:48 AM.

Details

Summary

Enforce the rule in C++11 [temp.mem]p2 that local classes cannot have
member templates.

This fixes PR16947.

N.B. C++14 has slightly different wording to afford generic lambdas
declared inside of functions.

Fun fact: Some formulations of local classes with member templates
would cause clang to crash during Itanium mangling, such as the
following:

void outer_mem() {

struct Inner {
  template <typename = void>
  struct InnerTemplateClass {
    static void itc_mem() {}
  };
};
Inner::InnerTemplateClass<>::itc_mem();

}

Diff Detail

Event Timeline

ygao added a comment.Oct 9 2013, 1:19 AM

This is essentially the same implementation as what I proposed a few weeks ago,
http://llvm-reviews.chandlerc.com/D575.
On that review, I was waiting to hear from Faisal who has started adding support
for local member templates to clang, and I am not sure if he needs this support
for any language extensions.

I feel that it is wrong to just remove tests without also removing relevant compiler
changes, because that leaves a feature in the compiler without any tests. e.g.,
If one wants to remove the following two tests, he/she should either revert r184903
or write some different tests.
test/PCH/cxx-local-templates.cpp
test/PCH/cxx1y-local-templates.cpp

Members which are templates inside of local classes can still exist with my
patch, they cannot be written explicitly though:

For example:
int fun() { auto L = [](const auto& x, auto& y){ return x + y; }; }

Gives an -ast-dump with:
`-FunctionDecl 0x6c8c480 <<stdin>:1:1, col:79> fun 'int (void)'
[...snip...]

|-CXXRecordDecl 0x6cd6490 <col:25> class definition
| |-FunctionTemplateDecl 0x6cd6740 <<invalid sloc>, col:76>

operator()

Given that this can still happen I am not eager to remove the code that
made these constructs work better.

I am of the belief that if we choose to accept this particular extension,
it should be explicitly OK'd in a draft or draft of a standard. EDG, GCC
and even MSVC all agree that this should not be permitted. I suppose if we
really want to support this feature, we should at least issue a diagnostic
under -pedantic.

A gentle ping.

majnemer closed this revision.Oct 21 2013, 9:18 PM
test/SemaTemplate/instantiate-exception-spec-cxx11.cpp