David Woo
David Woo

Reputation: 791

Invalid use of incomplete type in Clang if I declare member function template with arg of containing class

I'd like to implement a class like the one below with a member function template that takes a callable type F and applies it to the instance.

This compiles and runs in Visual Studio 15 but fails in clang with the error [x86-64 clang 4.0.0] error: invalid use of incomplete type 'Foo'

struct Foo
{
   template<typename F>
   auto applyFunctionToMe(F&& func)->decltype( func( Foo() ) )
   {
      return func( *this);
   }

   int contents;
};

int main()
{
   Foo bar;
   auto result = bar.applyFunctionToMe([](const Foo& f){ return f.contents;});
   return result;
}

Is there a way to get this to work under Clang in C++11? Which compiler is more correct according to standard?

Upvotes: 0

Views: 52

Answers (1)

0x5453
0x5453

Reputation: 13589

Try decltype(func(*this)) instead of decltype( func( Foo() ) )

I'm not sure whether or not this is standards-compliant, but clang apparently doesn't like using the Foo() constructor within the definition of Foo.

Upvotes: 1

Related Questions