sed-szeged / cppbackport Goto Github PK
View Code? Open in Web Editor NEWLicense: Other
License: Other
Basically, this:
#include <iostream>
#include <utility>
const char* f()
{
return "Hello World!";
}
template <typename F>
void puts(F&& fn)
{
std::cout << std::forward<F>(fn)() << "\n";
}
int main()
{
puts([] {
return f();
});
}
Yields this output:
#include <locale>
#include <iostream>
#include <utility>
const char* f()
{
return "Hello World!";
}
template <typename F>
void puts(F&& fn)
; /* Function Body Removed - Specialization generated */
/*1000000*/
template <>
void puts<(lambda at ~/git/llvm/temp/test.cpp:18:8)> ( (lambda at ~/git/llvm/temp/test.cpp:18:8) && fn ) {
std::cout << std::forward<(~/git/llvm/temp/test.cpp:18:8)>(fn)() << "\n";
}
int main()
{
puts([] {
return f();
});
}
While if I use IILE (Immediately-Invoked Lambda Expression) instead of passing the lambda to the puts
function I get what's actually decent output (that's also valid C++98):
#include <iostream>
const char* f()
{
return "Hello World!";
}
int main()
{
const auto x = [] {
return f();
}();
std::cout << x << "\n";
}
Becomes:
#include <locale>
#include <iostream>
const char* f()
{
return "Hello World!";
}
int main()
{
class LambdaFunctor__11_18 {
public:
static char const * lambdaFunc()
{
return f();
}
};
const char *const x = LambdaFunctor__11_18::lambdaFunc();
std::cout << x << "\n";
}
There's several things wrong with the first produced output. Most obviously that the lambda didn't get a functor class generated and uses of the lambda type replaced by the functor.
More subtly visible errors:
Looking at the code at ReplaceForRange.inc:69 you seem to be jumping through extra hoops to eliminate the intermediate __range
variable from being emitted.
This is non-conformant for two reasons:
This could be solved by emitting the equivalent of the following instead (C++17 spec, 6.5.4 [stmt.ranged]):
{
auto&& __range = <for-range-initializer>;
auto __begin = &__range[0] <or> __range.begin() <or> begin(__range);
auto __end = &__range[sizeof(__range)/sizeof(__range[0])] <or> __range.end() <or> end(__range);
for (; __begin != __end; ++__begin)
{
<for-range-declaration> = *__begin;
<for-statement>
}
}
Note that you're almost there already. I believe, from code inspection (haven't yet had the time to try), that all you need to do is to emit the C++03 equivalent of auto&& __range = <for-range-initializer>
preceding its use instead of replacing the uses of __range
with <for-range-initializer>
.
Edit: if a universal reference (auto&&
) is a problem: due to the above description of the usage of __range
I believe an l-value reference (auto& __range = ...
) would suffice as well, because no std::forward<decltype(__range)>
-like constructs are used.
A link to http://www.inf.u-szeged.hu/~beszedes/research/AHS16.pdf might be relevant.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.