For Programmers: Free Programming Magazines  


Home > Archive > VC Language > November 2005 > explicit member template function specialization









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author explicit member template function specialization
vrabetz@gmail.com

2005-11-23, 7:04 pm

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

Victor Bazarov

2005-11-23, 7:04 pm

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
vrabetz@gmail.com

2005-11-23, 7:04 pm

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.

Victor Bazarov

2005-11-23, 7:04 pm

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
Victor Bazarov

2005-11-23, 7:04 pm

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
vrabetz@gmail.com

2005-11-23, 7:04 pm

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.

Victor Bazarov

2005-11-23, 7:04 pm

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
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com