GithubHelp home page GithubHelp logo

cppbackport's People

Contributors

jocelyndelalande avatar sistvann avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

cppbackport's Issues

Lamda used as argument to template function causes illegal specialization to be emitted

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:

  • The main template (not the specialization) still contains an r-value/universal reference which is illegal C++98
  • The specialization also contains an r-value reference

Range initialization expression evaluated twice after range-based for transformation

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:

  1. You're no longer applying lifetime extension to the object returned by the range-init expression (section 12.2 [class.temporary], paragraph 5) and thus not ensuring that said object is still alive to iterate over after having extracted its iterators
  2. You're now evaluating the range-init expression twice, which is a problem if that expression is not idempotent. I.e. it might be possible for the second evaluation of the range-init expression to return a different object, and thus the two iterators would be part of different ranges and comparing them be meaningless. This is true for mostly every range-init expression that's an prvalue (e.g. any function returning a newly constructed range object).

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.