template question

Discussion in 'C++' started by John, Apr 15, 2011.

  1. John

    John Guest

    Hi,

    I have a templated class whose template parameter depends on a library.
    For most platforms, the library defines a certain type as an
    integer, so my original class was

    //library code
    typedef int SomeType;
    SomeType ATYPE = 1;

    //my code
    template <int T> class A {};
    ...
    A<ATYPE> a;

    However, on a few platforms, that type is a structure/class, so the
    above template fails since I need

    //library code
    struct X {};
    typedef struct X SomeType;
    SomeType ATYPE;

    //my code
    template <class T> class A {};
    ...
    A<ATYPE> a;


    template <class T> class A {};

    Which of course fails when the library defines the type as an integer.
    Is there anyway to make the template class accept both of these without
    resorting to #ifdef's?

    Thanks.
    John, Apr 15, 2011
    #1
    1. Advertising

  2. On 4/15/2011 12:40 PM, John wrote:
    > I have a templated class whose template parameter depends on a library.
    > For most platforms, the library defines a certain type as an integer, so
    > my original class was
    >
    > //library code
    > typedef int SomeType;
    > SomeType ATYPE = 1;
    >
    > //my code
    > template <int T> class A {};
    > ...
    > A<ATYPE> a;
    >
    > However, on a few platforms, that type is a structure/class, so the
    > above template fails since I need
    >
    > //library code
    > struct X {};
    > typedef struct X SomeType;
    > SomeType ATYPE;
    >
    > //my code
    > template <class T> class A {};
    > ...
    > A<ATYPE> a;
    >
    >
    > template <class T> class A {};
    >
    > Which of course fails when the library defines the type as an integer.
    > Is there anyway to make the template class accept both of these without
    > resorting to #ifdef's?


    Not really.

    Does your class need to have the *value* as its template argument?
    Perhaps you could rewrite it to make the constructor of your class
    accept that argument.

    If you're relying on the value/constant significantly and don't want to
    introduce another member, etc., then #ifdef is the simplest approach.

    You could try introducing some kind of framework that would *either* get
    your 'a' defined as an object of the template based on the value (if
    your 'SomeType' is an integral type, for instance) *or* as the
    constructor argument (if 'SomeType' is not an integral type, that is),
    but it could be quite convoluted, and I'm not sure you need that
    complexity. Disclaimer: I've not attempted that, my speculation on the
    complexity is just that, a speculation.

    V
    --
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Apr 15, 2011
    #2
    1. Advertising

  3. John

    john Guest

    On 4/15/2011 1:48 PM, Victor Bazarov wrote:
    > On 4/15/2011 12:40 PM, John wrote:
    >> I have a templated class whose template parameter depends on a library.
    >> For most platforms, the library defines a certain type as an integer, so
    >> my original class was
    >>
    >> //library code
    >> typedef int SomeType;
    >> SomeType ATYPE = 1;
    >>
    >> //my code
    >> template <int T> class A {};
    >> ...
    >> A<ATYPE> a;
    >>
    >> However, on a few platforms, that type is a structure/class, so the
    >> above template fails since I need
    >>
    >> //library code
    >> struct X {};
    >> typedef struct X SomeType;
    >> SomeType ATYPE;
    >>
    >> //my code
    >> template <class T> class A {};
    >> ...
    >> A<ATYPE> a;
    >>
    >>
    >> template <class T> class A {};
    >>
    >> Which of course fails when the library defines the type as an integer.
    >> Is there anyway to make the template class accept both of these without
    >> resorting to #ifdef's?

    >
    > Not really.
    >
    > Does your class need to have the *value* as its template argument?
    > Perhaps you could rewrite it to make the constructor of your class
    > accept that argument.
    >


    You are right. This is a better way to do it and avoids the problem.

    Thanks,
    John
    john, Apr 15, 2011
    #3
    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. Chris Theis
    Replies:
    2
    Views:
    465
    Chris Theis
    Jul 24, 2003
  2. tom_usenet
    Replies:
    0
    Views:
    532
    tom_usenet
    Jul 24, 2003
  3. Replies:
    1
    Views:
    2,101
    Gianni Mariani
    Jun 8, 2007
  4. Peng Yu
    Replies:
    3
    Views:
    767
    Thomas J. Gritzan
    Oct 26, 2008
  5. nguillot
    Replies:
    5
    Views:
    527
Loading...

Share This Page