Pointer-to-member-function as template parameter - strange error, plus an ICE

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance




Hi

Here's some code that makes the VC8 compiler do interesting things.
(In it's current form it doesn't really makes sense, I tried to strip
it down to the smallest possible code that still exhibits this
behavior.)


#include "stdafx.h"

template <class C, typename Sig>
struct Func;

template <class C, typename R, typename P1>
struct Func<C, R (P1)>
{
template <R (C::*)(P1)>
struct N;
};


template <class C, typename Sig>
struct F
{
template <class T>
static char Test(typename Func<C, Sig>::template N<&T::F>*);
};

template <class C, typename Sig>
struct G
{
typedef typename Func<C, Sig>::template N<&C::G> TD;
};


struct S
{
void G(int i) { }
int F(int i) { return 2; }
};


int _tmain(int argc, _TCHAR* argv[])
{
F<S, void (int)> h1; // 1
F<S, int (double)> h2; // 2
G<S, void (int)> h3; // 3
return 0;
}


This won't compile, saying that
error C2440: 'specialization' : cannot convert from 'overloaded-
function' to 'int (__thiscall S::* )(double)'
The error is in the typedef line, instantiated from the line marked
with 3.
The interesting thing is that that line uses the G template, which
only references the G() function in S. Where does the double argument
come from in the error message? Also, the message talks about an
overloaded function, but S::G() is not overloaded.

Some other interesting observations:
- If I delete any one of the 3 marked line in main(), then it
compiles
- If 1 is moved after 3, then it compiles
- If 2 is moved after 3, then it causes an Internal Compiler Error

Am I doing something wrong, or is it some kind of compiler bug?

Imre

.



Relevant Pages

  • Re: Incomplete type as template argument
    ... "Victor Bazarov" wrote in message ... >> struct B ... >> Could someone please explain to me when the compiler actually does ... >> the template expansion, and is this method of using incomplete types ...
    (comp.lang.cpp)
  • Some issues with using sizeof() in template definitions
    ... I have two issues with VC++, which might or might not be related, but both have to do with using the sizeof inside template definitions. ... When using sizeof inside my allocator, such as to assign to an enum value defined in the class, the compiler complains about errors in. ... templatestruct MemberCheck ...
    (microsoft.public.vc.language)
  • Re: Incomplete type as template argument
    ... > struct A; ... > void foo() ... > and is this method of using incomplete types as template arguments safe ... You should probably ask whoever made your compiler. ...
    (comp.lang.cpp)
  • possible compiler bug?
    ... I have a template class that instantiates a variable of the template ... struct TemplateClass ... struct TestClass ... it compiles fine under the Borland compiler. ...
    (comp.lang.cpp)
  • Re: komplexes Problem mit Funktionszeigern
    ... Die Funktion gibt einen struct "by value" zurueck und bei der ... am Borland Compiler, da der Microsoft Compiler ... Man kann Argumente fuer eine Funktion z.B auf dem Stack uebergeben. ... Das funktioniert natuerlich nur fuer "Basis Typen" die in ein Register passen. ...
    (de.comp.lang.c)