|
-
Jan 16th, 2002, 08:49 PM
#1
Thread Starter
transcendental analytic
FYI template partial specialation emulation in MSVC6 now possible
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 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 
PHP Code:
#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();
}
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Jan 16th, 2002, 10:16 PM
#2
Oooo.
But try:
PHP Code:
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?
-
Jan 17th, 2002, 10:10 AM
#3
So how would you write the compile time pow now?
PHP Code:
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?
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jan 17th, 2002, 12:44 PM
#4
Thread Starter
transcendental analytic
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:
PHP Code:
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
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Jan 21st, 2002, 05:30 PM
#5
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.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jan 21st, 2002, 07:09 PM
#6
Thread Starter
transcendental analytic
Well bad news is that it doesn't work in MSVC7, which gets me madder than ever. No Partial specialation and no workaraound either
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Jan 22nd, 2002, 07:55 AM
#7
It does work with 6 and doesn't with 7? 
And 7 hasn't got partial specialisation either? 
OMG, MS, what are you doing?????????
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jan 22nd, 2002, 06:17 PM
#8
Thread Starter
transcendental analytic
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
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Jan 22nd, 2002, 06:36 PM
#9
Monday Morning Lunatic
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.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Jan 23rd, 2002, 12:39 PM
#10
Monday Morning Lunatic
I've got the compiler running, and it works with kedaman's code.
Unfortunately, I haven't managed to get the linker working yet 
Nope, not a Linux emulator - it's an environment that attempts to simulate Unix but it still requires Windows for all the process management.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Jan 23rd, 2002, 02:10 PM
#11
But won't the linker of gcc3 (would be the GNU linker "ln") link to linux apps?
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jan 23rd, 2002, 02:14 PM
#12
Monday Morning Lunatic
Under Cygwin, ld still produces Win32 executables.
ld != GCC. It's a separate program, developed totally separately.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Jan 23rd, 2002, 02:41 PM
#13
but gcc uses ld as linker. Is there another version supplied with cygwin?
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jan 23rd, 2002, 02:43 PM
#14
Monday Morning Lunatic
There has to be a different compiler/linker for every architecture/OS combination.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Jan 24th, 2002, 08:18 AM
#15
keda: I just noticed that you forgot the template<> for the 1 specialisation of Max_Partial
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jan 24th, 2002, 10:50 AM
#16
Thread Starter
transcendental analytic
yeah, you are right
PHP Code:
template<>
struct Max_Partial<1>{
template <class A,class B>
struct Max_Partial1{
typedef B Max_Partial2;
};
};
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Feb 1st, 2002, 07:09 AM
#17
Thread Starter
transcendental analytic
BREAKING NEWS
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 
PHP Code:
#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;
}
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|