13
votes

While answering How do I write a lambda expression that looks like a method?, I tried to turn a captureless lambda into a member function pointer by exploiting the fact that, since C++17, captureless lambdas have a constexpr conversion operator to their function pointer type.

So I came up with an issue boiling down to:

template<void(*)()> struct A{};

int main()
{
  A<static_cast<void(*)()>([]{})>{}; // 1

  constexpr auto fp = static_cast<void(*)()>([]{});
  A<fp>{}; // 2
}

Now, this compiles in clang (since 5.0.0) but gcc(>=7.2) complains:

error: lambda-expression in template-argument
   A<static_cast<void(*)()>([]{ /*whatever*/ })>{}; // 1
                            ^
error: 'main()::<lambda()>::_FUN' is not a valid template argument for type 'void (*)()' because 'static constexpr void main()::<lambda()>::_FUN()' has no linkage
   A<fp>{}; // 2

The question is, who's right?

1
A simpler example using constexpr local variables should exist, no?Yakk - Adam Nevraumont
@Yakk, done; this is not entirely equivalent to the original code though ... or not ?Massimiliano Janes
I cannot help wondering what you are trying to do exactly and why this is needed.Germán Diago
Pointer to function with no linkage should be allowed to be a non-type template argument in C++17. See N4198.cpplearner
@massim not quite the same, but 3 times simpler. And I can see the use; fastest possible delegates the portable version.Yakk - Adam Nevraumont

1 Answers

6
votes

This is a gcc bug, filed 83258.

In C++14, we used to have a linkage requirement for non-type template parameters of pointer type. But in C++17 (as a result of N4268), the parameter just needs to be a converted constant expression of the correct type, with a few other restrictions (none of which are relevant here). Once we can construct fp, we should be able to use it as a template parameter.