We have a problem using a class template which itself uses function objects in some of its member functions. The error message form the VS2010 compiler is:
error C2512: ‘SimpleFunctor::SimpleFunctor’ : no appropriate default
constructor available
The downsized code to reproduce this is as follows:
// myfunctor.h
class SimpleFunctor
{
private:
SimpleFunctor( const SimpleFunctor& );
SimpleFunctor& operator=( const SimpleFunctor& );
public:
bool operator()() { return true; }
};
// mytemplate.h
#include "myfunctor.h"
template< typename T >
class Test
{
private:
Test( const Test& );
Test& operator=( const Test& );
public:
Test(){}
void testFunction( T parameter )
{
bool result = SimpleFunctor()();
}
};
// main.cpp
#include "HK_Template.h"
int main()
{
Test< int > obj;
obj.testFunction( 5 );
return 0;
}
This examples produces the above error message which seems to be correct since adding the default constructor to class SimpleFunctor like:
SimpleFunctor() {}
fixes the error.
So the question is, why does the compiler not generate the default constructor?
Once you define any constructor yourself, including a copy constructor, the compiler doesn’t generate a default constructor anymore.
(On the other hand, a copy/move constructor is generated by default if you don’t provide one, subject to certain rules.)