Re: Determining sub-template(?) parameter
Stuart Redmann a ?crit :
Stilgar wrote:
template< class T > struct complex_trait;
tempate<class T> struct complex_trait<complex<T> > { typedef T value; }
Sorry for interupting. I was following this thread with much interest,
but I can't follow your ideas any more. If I understand this right the
first line "template< class T > struct complex_trait;" declares a
template struct, but leaves the definition open.
the reason why I didn't define the struct is because there is no
coherent default value. so if someone try to use complex_trait with a
type for which she has not been specialized, it will result in a compile
time error:
so on VS2005:
the statement complex_trait< int >::value bb;
generate:
d:\prog\2006\tools\pdfextractor\pdfextractor.cpp(26) : error C2027: use
of undefined type 'complex_trait<T>'
with
[
T=int
]
d:\prog\2006\tools\pdfextractor\pdfextractor.cpp(26) : error C2065:
'value' : undeclared identifier
The second line,
"tempate<class T> struct complex_trait<complex<T> > { typedef T value;
}", defines the struct. Shouldn't it be "template<class T> struct
complex_trait {typedef T value;}"? (My copy of Stroustrup doesn't
mention this second occurance of the template parameter before the curly
braces that hold the template definition).
This methode is called partial template specialisation (I think). we
could have used:
template<> struct complex_trait< std::complex<float> > { typedef float
value; };
template<> struct complex_trait< std::complex<double> > { typedef double
value; };
but in doing so, we need to write a specialized complex_trait for each
specialisation of std::complexe.
if you have a compiler who support partial template specialisation (the
recent compilers do), you can write this:
template<class T> struct complex_trait< std::complex<T> > { typedef T
value; };
another example of this technique could be:
template< class A, class B >
void bar(A a,B b) { ... } // method 1
template< class A >
void bar(A a,float b) { ... } // method 1
....
bar( 1 ,2); // method 1
bar( 1 ,2.0f); // method 2
bar( 1.0 ,2.0f); // method 2
bar( 1 ,2.0); // method 1
perhaps this is clearer too you, but it's really the same things.
I hope this will be helpfull to you and that I didn't make too many
mistake with my poor english.
--
C?dric Venet
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]