PDA

Click to See Complete Forum and Search --> : FYI template partial specialation emulation in MSVC6 now possible


kedaman
Jan 16th, 2002, 07:49 PM
In an attempt to invent a compiletime nested type sorter (still working on it) I discovered a wonderful way to emulate partial specialation without even having to change the syntax, by actually nest a class with the unspecified parameters :D Now this case only shows how you can specialize one parameter, similarily if you need to specialize any parameter you have to nest each and one of them into the other and then create a specialized nested class where the needed parameters are specialized accordingly. This is just the beginnig of an extensive use of compiletime type selection, and as already mentioned, i'm doing a recursive sort algoritm that will resolve the type order at compile time :D

#include <iostream>
using namespace std;

template <int x>
struct Max_Partial{
template <class A,class B>
struct Max_Partial1{
typedef A Max_Partial2;
};
};
template <>
struct Max_Partial<0>{
template <class A,class B>
struct Max_Partial1{
typedef A Max_Partial2;
};
};
struct Max_Partial<1>{
template <class A,class B>
struct Max_Partial1{
typedef B Max_Partial2;
};
};
template <class A,class B,int x>
struct Max{
typedef Max_Partial<x>::Max_Partial1<A,B>::Max_Partial2 TMax;
};


struct A{
static const char* me(){return "class A";};
};
struct B{
static const char* me(){return "class B";};
};

void main(){
cout << Max<A,B,1>::TMax::me();
cout << Max<A,B,0>::TMax::me();
}

jim mcnamara
Jan 16th, 2002, 09:16 PM
Oooo.

But try:

cout<<Max<(void *)a,(void *)b, (void *)c>::Tmax::me();




I get 'unrecoverable compiler error at line 78'

It really doesn't seem to like void?..... OR did I type wrong?

CornedBee
Jan 17th, 2002, 09:10 AM
So how would you write the compile time pow now?

template <int a>
struct cpow {
template <int b>
struct cpow_inner {
enum { val = cpow<a>::cpow_inner<b-1>::val * a};
};
};

template <int a>
template <>
struct cpow<a>::cpow_inner<0> {
enum { val = a };
};


Did I get this right?

kedaman
Jan 17th, 2002, 11:44 AM
I don't know what your compiler says about template<int a> template<> kind of declaration but It certainly looks weird, in msvc with the same base you can nest the template specification classes no matter of scope, so it would look something like this:

template <int a>
struct cpow {
template <int b>
struct cpow_inner {
enum { val = cpow<a>::cpow_inner<b-1>::val * a};
};

template <>
struct cpow_inner<0> {
enum { val = 1 };
};
};

and a^0 = 1, not a ;)

CornedBee
Jan 21st, 2002, 04:30 PM
aahh, I didn't know you can write specialisations inside the outer template.

Damn right about the second thing, I guess I was too tired.

kedaman
Jan 21st, 2002, 06:09 PM
Well bad news is that it doesn't work in MSVC7, which gets me madder than ever. No Partial specialation and no workaraound either :mad:

CornedBee
Jan 22nd, 2002, 06:55 AM
It does work with 6 and doesn't with 7? :D
And 7 hasn't got partial specialisation either? :eek:
OMG, MS, what are you doing?????????

kedaman
Jan 22nd, 2002, 05:17 PM
I have no Idea but it doesn't look good :/ I'm counting on that parksie is going to get GCC3 work in windows environment :p

parksie
Jan 22nd, 2002, 05:36 PM
I dunno what the hell MS are doing, but when I get time in a few days I'll continue my efforts to get GCC running properly.

It compiles now, but I haven't got the linker working yet.

parksie
Jan 23rd, 2002, 11:39 AM
I've got the compiler running, and it works with kedaman's code.

Unfortunately, I haven't managed to get the linker working yet :rolleyes: :D

Nope, not a Linux emulator - it's an environment that attempts to simulate Unix but it still requires Windows for all the process management.

CornedBee
Jan 23rd, 2002, 01:10 PM
But won't the linker of gcc3 (would be the GNU linker "ln") link to linux apps?

parksie
Jan 23rd, 2002, 01:14 PM
Under Cygwin, ld still produces Win32 executables.

ld != GCC. It's a separate program, developed totally separately.

CornedBee
Jan 23rd, 2002, 01:41 PM
but gcc uses ld as linker. Is there another version supplied with cygwin?

parksie
Jan 23rd, 2002, 01:43 PM
There has to be a different compiler/linker for every architecture/OS combination.

CornedBee
Jan 24th, 2002, 07:18 AM
keda: I just noticed that you forgot the template<> for the 1 specialisation of Max_Partial

kedaman
Jan 24th, 2002, 09:50 AM
yeah, you are right

template<>
struct Max_Partial<1>{
template <class A,class B>
struct Max_Partial1{
typedef B Max_Partial2;
};
};

kedaman
Feb 1st, 2002, 06:09 AM
Partial Specialisation still not available in MSVC7 but I managet to tweak it to work again, you can now safely emulate PS again :)
BTW the IDE that comes with VS.NET is truly amazing :)

#include <iostream>
using namespace std;
template <class A,class B> struct Selector_Partial_Outher{
template<int C> struct Selector_Partial_Inner{
typedef B P;
};
template<>struct Selector_Partial_Inner<0>{
typedef A P;
};
};

template <class A,class B,int C> struct Selector{
typedef Selector_Partial_Outher<A,B>::Selector_Partial_Inner<C>::P P;
};

struct A{static const int message=1;};//YES YOU CAN DO IT NOW IN MSVC BUT ONLY WITH CONST INTEGRAL MEMBERS
struct B{static const int message=2;};

int main(){
cout<<Selector<A,B,0>::P::message<<endl;
cout<<Selector<A,B,1>::P::message<<endl;
}