Should the function pointer point to a function with an external connection when using a template as a parameter?

In the code below, in the first form, gcc complains about the presence of lambda in the template parameter. In the second form, gcc complains that lambda_function_pointer has no external connection. Clang compiles and runs the code just fine, even with -pedantic.

+ before lambda is to make it decompose into a function pointer.

 template<auto f> void func() { f(); } void g(); int main() { func<+[](){}>(); // gcc complains about lambda in template args constexpr auto lambda_function_pointer = +[](){}; func<lambda_function_pointer>(); // gcc complains about not having external linkage } 

live: https://godbolt.org/g/ey5uo7

Thanks.

edit: https://timsong-cpp.imtqy.com/cppwp/n4659/expr.prim.lambda#2 mentions lambdas that do not appear in the template parameters, since lambda is not in the signature, but with + it gets rid of lambda- type.

edit2: This may be relevant for the link part of the question: Why does C ++ 03 require that the template parameters have external binding?

+8
c ++ lambda templates
source share
1 answer

func<+[](){}> poorly formed in C ++ 17 for the exact paragraph you are associated with. The non-harmonic note simply explains the motivation of the regulatory prohibition. This does not mean - and cannot - limit it. This restriction has been removed in the current working draft P0315 , so it has a good chance to make C ++ 20.

Pre-C ++ 17, a lambda expression cannot be evaluated inside constant expressions.

The "linkage" part is a duplicate. Can I use the conversion result without restrictions of lambda constexpr in C ++ 17 as an argument to a function pointer template? . This is a GCC error.

+3
source share

All Articles