Comments (4)
Hello @Ukilele,
re 2.)
Thanks for that idea. Sadly, even with getDescribedFunctionTemplate
, the transformation remains guesswork, and there is at least one scenario where it fails.
The results for TestA
and TestC
are different. In TestC
, I can guess, with the help of storing state, that a SubstNonTypeTemplateParmExpr
comes from a NonTypeTemplateParmDecl
, which shares the same DeclRefExpr
as used prior for defining the CXXFoldExpr
in the primary template. I'm not even sure in how many cases this guessing fails, but at least for this case, it would work.
Everything is different for TestA
. Here, we are looking at a ParmVarDecl
in the instantiation. As far as I can tell, this decl shares nothing with the decl in the CXXFoldExpr
.
With the guessing, the effort, and the situation that it would still not work in all cases, I do not plan to do anything here. However. as always, PRs are welcome.
Andreas
from cppinsights.
I quickly checked that at least for the operators ,
(comma), +
(plus) and &
(bit and) the behavior is the same.
from cppinsights.
Hello @Ukilele,
thanks for reporting this issue!
I agree with your quote from the standard. My issue is that at the point I render something like 4, 5, 6
, I don't know whether this results from a fold-expression or a user's code. Looking at the AST using Compiler Explorer compiler-explorer.com/z/MYWb9T3xq shows in line 77 the BinaryOperator
. It does not say anything about a fold expression. I thought I could use NonTypeTemplateParmDecl
in line 80, but this also doesn't know how the pack is used later. I would be happy if you or anybody else knows how to get this information.
C++ Insights introduces parens in some cases, for example
Line 720 in 50b962d
I wonder how your test looked for +
because my example works:
template<typename... Ts>
constexpr auto avg(const Ts&... vals)
{
return (vals + ...) / sizeof...(vals);
}
static_assert(avg(2, 3, 4) == 3);
This translates to
template<>
inline constexpr unsigned long avg<int, int, int>(const int & __vals0, const int & __vals1, const int & __vals2)
{
return (static_cast<unsigned long>(__vals0 + (__vals1 + __vals2))) / 3;
}
As much as it bugs me, this issue will remain unfixed for now.
Andreas
from cppinsights.
Hello @andreasfertig,
1.) You're welcome. Thanks so much for this helpful tool! 🚀
2.) Yes, it seems that once you have a concrete FunctionDecl
(even if it is the result of instantiating a FunctionTemplateDecl
),
all information about the contained fold expression is lost. Maybe via getDescribedFunctionTemplate you can get from the FunctionDecl
to its FunctionTemplateDecl
, which does contain the CXXFoldExpr
we need. Still, it sounds like this issue is quite tedious to resolve.
3.) Thanks for pointing at const bool needLHSParens{isa<BinaryOperator>(stmt->getLHS()->IgnoreImpCasts())};
, I only just now realized that cppinsights transforms 1 + 2 + 3
into (1 + 2) + 3
.
I guess this explains the parentheses that cppinsights generates in the instantiations of my TestC
and TestD
functions.
4.) My test for +
looked the way, that I took the original code I mentioned in the issue and replaced each occurrence of the fold-operator ,
by +
.
Best regards,
Kilian
from cppinsights.
Related Issues (20)
- for-loops as while-loops is wrong for ranged-based for loop HOT 1
- for-loops as while-loops is wrong for ranged-based for loop HOT 1
- for-loops as while-loops is wrong for ranged-based for loop HOT 1
- Can I use #include <Windows.h> with cppinsights? HOT 4
- coroutine transformation: Support _promise-constructor-arguments_ HOT 5
- [BUG] with getting a global variable HOT 1
- Generated insight for `jmp_buf` global variable declarations. HOT 1
- invoke lambda with `temp.operator()()` generate wrong code. HOT 1
- It does not show vtable and vptr assignment HOT 2
- Error building inside clang source tree HOT 1
- About nested class inside a class template HOT 1
- Specialization of variable templates misses type deduction and `template` HOT 1
- Would it possible to have more educational things supported? HOT 6
- Site is down HOT 3
- cppinsights.io doesn't show the what is listed in github.com HOT 1
- Errors in transforming `struct T {} var` HOT 2
- About the release scheme HOT 1
- Errors in transforming "int& a = A()" HOT 1
- Pack expansion in ctor call transformed into invalid c++
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cppinsights.