Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Re: Template specialisation problem

Reply
Thread Tools

Re: Template specialisation problem

 
 
Lars Yencken
Guest
Posts: n/a
 
      08-20-2003
"ES Kim" <(E-Mail Removed)> writes:
> Specialization for float is:
>
> template < >
> bool checkType<float>(float type )

Here the argument passed is actually of type DataType rather than float, where
DataType is defined elsewhere as an enum of some sort.

> {
> return isScalarType( type );
> }
>
>
> --
> ES Kim


I tried this, but got the following error (BigParser.h is the only place this
code is used, in another template).

src error LNK2005: "bool __cdecl checkType<float>(enum DataType)" (?checkType@?$@M@@YA_NW4DataType@@@Z) already defined in BigParser.obj

Lars
 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      08-20-2003
> >
> > template < >
> > bool checkType<float>(float type )

> Here the argument passed is actually of type DataType rather than float,

where
> DataType is defined elsewhere as an enum of some sort.
>
> > {
> > return isScalarType( type );
> > }
> >
> >
> > --
> > ES Kim

>
> I tried this, but got the following error (BigParser.h is the only place

this
> code is used, in another template).
>
> src error LNK2005: "bool __cdecl checkType<float>(enum DataType)"

(?checkType@?$@M@@YA_NW4DataType@@@Z) already defined in BigParser.obj
>
> Lars


Well at least it compiled, Try this

template < >
inline bool checkType<float>(float type )
{
return isScalarType( type );
}

Note the use of inline.

john


 
Reply With Quote
 
 
 
 
Lars Yencken
Guest
Posts: n/a
 
      08-20-2003
Lars Yencken <(E-Mail Removed)> writes:

> "ES Kim" <(E-Mail Removed)> writes:
> > Specialization for float is:
> >
> > template < >
> > bool checkType<float>(float type )

> Here the argument passed is actually of type DataType rather than float, where
> DataType is defined elsewhere as an enum of some sort.
>
> > {
> > return isScalarType( type );
> > }
> >
> >
> > --
> > ES Kim

>
> I tried this, but got the following error (BigParser.h is the only place this
> code is used, in another template).
>
> src error LNK2005: "bool __cdecl checkType<float>(enum DataType)" (?checkType@?$@M@@YA_NW4DataType@@@Z) already defined in BigParser.obj
>
> Lars


Aha!!!! I used your suggestion but inlined it and it all works fine.

Thanks a lot!

Lars

 
Reply With Quote
 
ES Kim
Guest
Posts: n/a
 
      08-20-2003
"Lars Yencken" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> "ES Kim" <(E-Mail Removed)> writes:
> > Specialization for float is:
> >
> > template < >
> > bool checkType<float>(float type )

> Here the argument passed is actually of type DataType rather than float,

where
> DataType is defined elsewhere as an enum of some sort.
>
> > {
> > return isScalarType( type );
> > }
> >
> >
> > --
> > ES Kim

>
> I tried this, but got the following error (BigParser.h is the only place this
> code is used, in another template).
>
> src error LNK2005: "bool __cdecl checkType<float>(enum DataType)"

(?checkType@?$@M@@YA_NW4DataType@@@Z) already defined in BigParser.obj
>
> Lars


Sorry, I misread your posting. The template type argument T
is not used in function argument list. Your code is like this form:

template<typename T> void f(int i) { }
int main() { f(1); }

Every complier I tried rejected this including Comeau.
I wonder why your function is not of the form:

template<typename T>
bool checkType(T type);

--
ES Kim


 
Reply With Quote
 
llewelly
Guest
Posts: n/a
 
      08-20-2003
"ES Kim" <(E-Mail Removed)> writes:

> "Lars Yencken" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> "ES Kim" <(E-Mail Removed)> writes:
>> > Specialization for float is:
>> >
>> > template < >
>> > bool checkType<float>(float type )

>> Here the argument passed is actually of type DataType rather than float,

> where
>> DataType is defined elsewhere as an enum of some sort.
>>
>> > {
>> > return isScalarType( type );
>> > }
>> >
>> >
>> > --
>> > ES Kim

>>
>> I tried this, but got the following error (BigParser.h is the only
>> place this code is used, in another template).
>>
>>
>> src error LNK2005: "bool __cdecl checkType<float>(enum DataType)"

> (?checkType@?$@M@@YA_NW4DataType@@@Z) already defined in BigParser.obj
>>
>> Lars

>
> Sorry, I misread your posting. The template type argument T
> is not used in function argument list. Your code is like this form:
>
> template<typename T> void f(int i) { }
> int main() { f(1); }


Such a function template is instantiated and called like this:

f<int>(1);


 
Reply With Quote
 
Lars Yencken
Guest
Posts: n/a
 
      08-21-2003
ES Kim wrote:
> Yes, it is. But what's the point of using template when template type
> argument T is not used anywhere in the function body?
> Only the OP knows.
>


It's a way of checking that the template type used elsewhere (and passed in
as T) correctly matches the DataType enum given as an argument. This code
is needed to make more interesting templated code work.

Cheers,
Lars
 
Reply With Quote
 
Lars Yencken
Guest
Posts: n/a
 
      08-21-2003
John Harrison wrote:
> Well at least it compiled, Try this
>
> template < >
> inline bool checkType<float>(float type )
> {
> return isScalarType( type );
> }
>
> Note the use of inline.
>
> john


Thanks John, that's just what I did and it fixed all my problems

Cheers,
Lars
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem with template specialisation Xavier Serrand C++ 7 07-12-2007 12:35 AM
Template partial specialisation/inheritance problem Jules C++ 6 08-18-2005 07:37 PM
Template class partial specialisation problem Lionel B C++ 7 11-04-2004 10:43 AM
Specialisation of a template that results to another template Nicolas Weidmann C++ 1 02-17-2004 10:18 AM
Template specialisation of class with base class template ?! Tim Clacy C++ 12 12-03-2003 05:51 PM



Advertisments