Weird behavior in MSVC 2005 SP1

Posted by: Jason Hughes in programming on  

So, during a refactor of a large section of code, I ran into an oddity with Microsoft's Visual Studio compiler in the way templates, default parameters, and namespace/class works. Really odd, particularly because it had been my (naive) understanding that a namespace and a class are quite close to the same thing if your functions are all declared to be static inside the class... truthfully, I (and most people I've talked to) believe that a class wrapping a bunch of static functions is just a nice way to force the user to use ClassName:: in front of your function calls. Well, I ran into an issue with this just a few days ago... don't recall what it was. But by running into a second one today, I thought it was worthwhile posting about it.


typedef TDelegate0<void> MyDelegate;

void
MyFunc(void) { }
class
C
{

public
:
static
void Fn(MyDelegate d = MyDelegate::FromFn<myfunc>()) {}
};


namespace
NS
{

static
void Fn(MyDelegate d = MyDelegate::FromFn<myfunc>()) {}
};


void
main(void)
{

C::Fn(); // error deducing template argument
NS::Fn(); // no problem

}


Don't worry too much about the template typedef itself--it's basically a wrapper for a callback function. The important thing to notice is that, at least with Visual Studio 2005 SP1, these are not equivalent constructs. Of course, I only came across this when I tried to make a function take a default parameter and ran into all kinds of trouble with deducing template arguments. When I simplified the code down, I did so with a namespace (by accident) and had no trouble at all. Just changing the namespace keyword to class caused the issue to pop up. Sigh.

Why can't we have nice things?