[cfe-dev] PR27015 (variable template initialized with a generic lambda expresssion)

Richard Smith via cfe-dev cfe-dev at lists.llvm.org
Tue Apr 12 17:34:38 PDT 2016


On Tue, Apr 12, 2016 at 5:22 PM, Akira Hatanaka via cfe-dev <
cfe-dev at lists.llvm.org> wrote:

> I was wondering whether someone could answer a few questions about
> variable templates. I'm trying to come up with a patch that fixes the crash
> described in PR27015.
>
> https://llvm.org/bugs/show_bug.cgi?id=27015
>
> The crash happens when clang compiles a code that has a variable template
> initialized with a generic lambda. For example,
>
> $ cat test1.c
>
> template<typename T> auto fn = [](auto a) { return a + T(1); };
>
> template <typename X>
> int func() {
>   X a = 0x61;
>   fn<char>(a);
>   return 0;
> }
>
> int main() {
>   func<int>();
> }
>
> First question, is this legal c++14 code? I didn't find anything that
> suggests it isn't legal, but I haven't found any code that uses variable
> templates like this either other than the provided test case.
>

Yes, sadly, this is valid C++14 code.


> Second question, what would the closure type look like in this case? My
> understanding is that the closure type for generic lambda without template
> parameters looks like this:
>
> class Class {
> template<typename AT>
>   AT operator()(AT a) { ... }
>   ...
> };
>
> With template parameter, would it look like this?
>
> template<typename T>
> class Class {
> template<typename AT>
>   AT operator()(AT a) { return a + T(1); }
>   ...
> };
>

No. Each instantiation of 'fn' gets its own closure type. The closure type
from the template itself should be treated as being in a dependent context,
even though there is no dependent DeclContext to contain it.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20160412/1f9d0d0b/attachment.html>


More information about the cfe-dev mailing list