Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > template question

Reply
Thread Tools

template question

 
 
John
Guest
Posts: n/a
 
      04-15-2011
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.
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      04-15-2011
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
 
Reply With Quote
 
 
 
 
john
Guest
Posts: n/a
 
      04-15-2011
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
 
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
r H2 deduce deduce template argument of a template class inheritingfrom a non template base? nguillot C++ 5 03-08-2009 05:56 PM
How to use the template member function of a template in the memberfunction of another template class? Peng Yu C++ 3 10-26-2008 03:51 PM
template template arguments: expected a class template, got `Component<T1, T2, T3> gary.bernstein@gmail.com C++ 1 06-08-2007 07:10 AM
Re: A Newbie Question about template template template tom_usenet C++ 0 07-24-2003 12:06 PM
Re: A Newbie Question about template template template Chris Theis C++ 2 07-24-2003 09:42 AM



Advertisments