FIX: C2784 Error When You Try to Compile a Template Function That Takes a Function Reference (309700)



The information in this article applies to:

  • Microsoft Visual C++ .NET (2002)

This article was previously published under Q309700

SYMPTOMS

The compiler generates a C2784 error when you try to create an instance of a template function that takes a function reference as an argument.

STATUS

Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article. This bug was corrected in Microsoft Visual C++ .NET (2003).

MORE INFORMATION

To reproduce this problem, try to build the following sample code:
#include <iostream>
using namespace std;

void func1( int (&fn)( float ) ) { cout << "hello" << endl; }

template<class R, class T>
void func2( R (&fn)( T ) ) { cout << "goodbye" << endl; }

int myfunc( float ) { return 0; }

int main()
{
	func1( myfunc ); // works fine
	func2( myfunc ); // fails with C2784
	return 0;
}
				
To work around this problem, use a function pointer instead of a function reference in the template definition. For example:
template<class R, class T>
void func2( R (*fn)( T ) ) {cout << "goodbye" << endl; }
				

Modification Type:MajorLast Reviewed:4/11/2003
Keywords:kbfix kbbug kbpending KB309700