Re: Template specialisation problem

Discussion in 'C++' started by Lars Yencken, Aug 20, 2003.

  1. Lars Yencken

    Lars Yencken Guest

    "ES Kim" <> 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
     
    Lars Yencken, Aug 20, 2003
    #1
    1. Advertising

  2. > >
    > > 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
     
    John Harrison, Aug 20, 2003
    #2
    1. Advertising

  3. Lars Yencken

    Lars Yencken Guest

    Lars Yencken <> writes:

    > "ES Kim" <> 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 :D
     
    Lars Yencken, Aug 20, 2003
    #3
  4. Lars Yencken

    ES Kim Guest

    "Lars Yencken" <> wrote in message
    news:...
    > "ES Kim" <> 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
     
    ES Kim, Aug 20, 2003
    #4
  5. Lars Yencken

    llewelly Guest

    "ES Kim" <> writes:

    > "Lars Yencken" <> wrote in message
    > news:...
    >> "ES Kim" <> 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);
     
    llewelly, Aug 20, 2003
    #5
  6. Lars Yencken

    Lars Yencken Guest

    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
     
    Lars Yencken, Aug 21, 2003
    #6
  7. Lars Yencken

    Lars Yencken Guest

    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
     
    Lars Yencken, Aug 21, 2003
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tim Clacy
    Replies:
    12
    Views:
    615
    Dan W.
    Dec 3, 2003
  2. Nicolas Weidmann
    Replies:
    1
    Views:
    311
    Christian Jaeger
    Feb 17, 2004
  3. Lionel B
    Replies:
    7
    Views:
    1,431
    Lionel B
    Nov 4, 2004
  4. Jules
    Replies:
    6
    Views:
    450
    Jules
    Aug 18, 2005
  5. Xavier Serrand

    problem with template specialisation

    Xavier Serrand, Jul 11, 2007, in forum: C++
    Replies:
    7
    Views:
    574
    Lionel B
    Jul 12, 2007
Loading...

Share This Page