Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
muggenhor opened this issue Mar 14, 2018 · 1 comment

Comments

@muggenhor
Copy link

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
@muggenhor
Copy link
Author

FYI: removing both (not just either of) the universal reference (F&& -> F) and the std::forward, from puts, completely makes this problem go away.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant