Hi,

I have a problem with a template function inside a class (no template).

The template parameter of the class is a policy class.

But the explicit specialization of the template function does not work.


Visual C++ 7.1 compiler said that the function cannot be explicitly
specialized.
But the same code compiles with the Comeau online compiler
successfully.

Here's a simplyfied version of the code (with same effect Comeau -> ok,
VC++ 7.1 -> error):


class Policy1
{
public:
typedef int type;
};

class Policy2
{
public:
typedef long type;
};


class ClassX
{
public:
template<typename PolicyT>
bool func(typename PolicyT::type arg);
};

template<>
bool ClassX::func<Policy1>(Policy1::type arg)
{
/* do something */
return true;
}

template<>
bool ClassX::func<Policy2>(Policy2::type arg)
{
/* do something */
return true;
}

int main()
{
ClassX x;
x.func<Policy1>(12);
}


Can anyone tell me, why this does not work (with VC++7.1)?

Thx in advance,
Georg

Re: explicit member template function specialization by Victor

Victor
Wed Nov 23 11:30:09 CST 2005

vrabetz@gmail.com wrote:
> Hi,
>
> I have a problem with a template function inside a class (no template).
>
> The template parameter of the class is a policy class.
>
> But the explicit specialization of the template function does not work.
>
>
> Visual C++ 7.1 compiler said that the function cannot be explicitly
> specialized.
> But the same code compiles with the Comeau online compiler
> successfully.
>
> Here's a simplyfied version of the code (with same effect Comeau -> ok,
> VC++ 7.1 -> error):
>
> [...]
> Can anyone tell me, why this does not work (with VC++7.1)?

Because VC++ v7.1 was (and is and forever will be) a bit behind on C++
templates implementation. I mean, do you really need to know _why_ it
doesn't compile or do you want to work around it or do you want to know
what Microsoft compiler will accept your code? Hint: VC++ v8 compiles
it without a problem.

V

Re: explicit member template function specialization by vrabetz

vrabetz
Wed Nov 23 12:01:41 CST 2005

Thank you for your prompt answer.

Actually I wanted to know how I can work around this problem with my
compiler.
So, do you have an idea how I can do this? Is it actually possible or
do I need to redesign my class?

Georg.


Re: explicit member template function specialization by Victor

Victor
Wed Nov 23 12:16:32 CST 2005

vrabetz@gmail.com wrote:
> Thank you for your prompt answer.
>
> Actually I wanted to know how I can work around this problem with my
> compiler.

You should have said so.

> So, do you have an idea how I can do this? Is it actually possible or
> do I need to redesign my class?

I am not sure this fits, but here is _a_ work-around:
-------------------------------------------------------
class Policy1
{
public:
typedef int type;
};

class Policy2
{
public:
typedef long type;
};


class ClassX
{
public:
template<typename T>
bool func(T arg);
};

template<>
bool ClassX::func<>(Policy1::type arg)
{
/* do something */
return true;
}

template<>
bool ClassX::func<>(Policy2::type arg)
{
/* do something */
return true;
}

int main()
{
ClassX x;
x.func<Policy1::type>(12);
}
--------------------------------------------------------

V

Re: explicit member template function specialization by Victor

Victor
Wed Nov 23 12:19:02 CST 2005

vrabetz@gmail.com wrote:
> Actually I wanted to know how I can work around this problem with my
> compiler.
> So, do you have an idea how I can do this? Is it actually possible or
> do I need to redesign my class?

----------------------------------------------- here is another
class Policy1
{
public:
typedef int type;
};

class Policy2
{
public:
typedef long type;
};


class ClassX
{
public:
template<typename P, typename T>
bool func(T arg);
};

template<>
bool ClassX::func<Policy1>(Policy1::type arg)
{
/* do something */
return true;
}

template<>
bool ClassX::func<Policy2>(Policy2::type arg)
{
/* do something */
return true;
}

int main()
{
ClassX x;
x.func<Policy1>(12);
}
----------------------------------------------- here is another

V

Re: explicit member template function specialization by vrabetz

vrabetz
Wed Nov 23 13:04:03 CST 2005

thank you very much.

my compiler accepts both work arounds, but in my case the second one
fits better and i'll use this one however i don't understand why this
one works.
but i think this question cannot be answered in a certain way, can it?

so once again thank you for your help...

Georg.


Re: explicit member template function specialization by Victor

Victor
Wed Nov 23 13:08:42 CST 2005

vrabetz@gmail.com wrote:
> thank you very much.
>
> my compiler accepts both work arounds,

I wouldn't post them without testing first...

> but in my case the second one
> fits better and i'll use this one however i don't understand why this
> one works.
> but i think this question cannot be answered in a certain way, can it?

What exactly do you need to know about "why this one works"? My guess is,
of course, that between the one that works and your original code lies the
border separating what VC++ folks had implemented from what they decided
to leave unimplemented in version 7.1 template handling. Is that a valid
answer to your "why" question?

V