[cfe-dev] c++14 vs c++17 different ast for lambda?

Jesse Zhang via cfe-dev cfe-dev at lists.llvm.org
Tue May 4 19:56:18 PDT 2021


I think this is intentional: a lambda expression returns a prvalue, which is

1. then used to copy-construct the argument in C++14, but
2. directly initialized the argument (guaranteed copy elision) in C++ 17

Jesse

On Tue, May 4, 2021 at 6:53 PM Evgeniy Dushistov via cfe-dev
<cfe-dev at lists.llvm.org> wrote:
>
> Hi,
>
> I am trying to investigate bug in the tool based on
> libclang. The problem is that with -std=c++14 and -std=c++17
> for code like this:
>
> #define signals public
> class QObject {
> signals:
>         void destroyed();
> public:
>         template<typename F>
>         static void connect(QObject *, void (QObject::*mf)(void), F f) {}
>         void deleteLater();
> };
>
> void test(QObject *o1, QObject *o2)
> {
>     QObject::connect(o1, &QObject::destroyed,
>                     [=] { o2->deleteLater(); });
> }
>
> clang dumps different AST. Is this intentional because of
> difference in handling of lambdas in c++14 and c++17 or this is
> some kind of bug?
>
> In case of -std=c++14 (clang++ -std=c++14 -Xclang -ast-dump -c test.cpp)
> 3rd argument (lambda) passed to QObject::connect looks like this:
> CXXConstructExpr 0x5589e3278970 <line:14:21, col:46> '(lambda at test.cpp:14:21)':'(lambda at test.cpp:14:21)' 'void ((lambda at test.cpp:14:21) &&) noexcept' elidable
>           `-MaterializeTemporaryExpr 0x5589e3275cd0 <col:21, col:46> '(lambda at test.cpp:14:21)' xvalue
>             `-LambdaExpr
>
> in case of -std=c++17 lambda looks like this:
>    `-LambdaExpr 0x5641f55db880 <line:14:21, col:46> '(lambda at test.cpp:14:21)'
>         |-CXXRecordDecl 0x5641f55db420 <col:21> col:21 implicit class definition
>
> --
> /Evgeniy
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at lists.llvm.org
> https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev


More information about the cfe-dev mailing list