Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lambda passed to template not defined

I was playing with C++17's class template argument deduction today. The first obvious idea that came to mind was passing a callable as template parameter. A callable, that's among other things a lambda, why not. Let's try that.

template<typename F> class foo { F f; public:     foo(F in) : f(in) { f(); /* not very useful, admitted */ } };  void bar() { puts("a"); }  int main() {     auto a = foo(bar);     auto b = foo([](){ puts("b"); });      return (void) a, (void) b, 0; } 

Here is what clang (5.0, r300688) has to say about it:

warning: function '<(lambda at [source location])>' has internal linkage but is not defined

The code compiles and certainly "works fine" but the warning suggests the compiler is not altogether happy with it.

I'm ready to agree that the lambda has internal linkage (being anonymous it's not accessible elsewhere in the same translation unit, so sure enough it's inaccessible in another one), but what about it. I don't want to access it from another translation unit.
The part about lacking definition strikes me as funny, I wouldn't even know how to write a lambda without defining it.

In summary: What gives? What to make of that? I don't like warnings, not only do they make the build less pretty, but they usually mean something is wrong and undefined behavior of sorts may bite you soon. On the other hand, how can I make a lambda more defined than it already is by writing out its definition?

like image 818
Damon Avatar asked Apr 20 '17 08:04

Damon


Video Answer


1 Answers

It seems to me this is a compiler glitch. Using Clang compiler of Visual Studio 2017 only this error is generated "cannot refer to class template 'foo' without a template argument list" for a and b instantiation in main function. If the function type is specified as template parameter, there are no warnings and no errors.

like image 94
Flaviu Avatar answered Sep 28 '22 04:09

Flaviu