468,491 Members | 1,950 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,491 developers. It's quick & easy.

templates - nontype parameters - specialization

10
Can I create a specialization of a single nontype parameter of a templated function of a non-templated class?? For example :

class A
{
...

template <int i> Handle( void * p) {}
template <> Handle<4>( void * p)
{ ... }

}

Correct me if I'm wrong with the syntax, but I've yet to find a good reference for this, and the compiler always throws at me useless template errors
Feb 3 '08 #1
7 3643
weaknessforcats
9,207 Expert Mod 8TB
int i is not a tyoe so you can't use it ro specialize a template. Neither is 4. SO these lines are just wrong:
template <int i> Handle( void * p) {}
template <> Handle<4>( void * p)
{ ... }
plus there is no object name.

Your class A has a Handle as a member. If it is a Handle<int>, then say so:
Expand|Select|Wrap|Line Numbers
  1. class A
  2. {
  3.     Handle<int> v1;
  4. };
  5.  
If the Handle has a constructor, it will be called when an object of A is created. That will require a constructor on A to call the constructor on the Handle.

LIke this:
Expand|Select|Wrap|Line Numbers
  1. class Handle
  2. {
  3.    public:
  4.       Handle(void*) {}
  5.  
  6. };
  7.  
  8. class A
  9. {
  10. //...
  11.     void* p;
  12. public:
  13.     A() : v1(p) {}
  14.  
  15. Handle<int>  v1;
  16.  
  17. };
  18. int main()
  19. {
  20.  
  21.     A obj;
  22.  
  23. }
  24.  
Finally, if this handle is for what I think it is, then there is one already set uop for you in the C/C++ HowTos in an article about Handle classes. Just copy out the template and off you go.
Feb 3 '08 #2
esrever
10
Thanks for your reply, but I think you totally misunderstood me. Handle is a member function of A, while the templated parameter I need it to be a non-type parameter. All the examples I've seen so far,have also a type, like the usual :

template <class T, int Size = 100>
class Stack
{
...
}

But I wanted to ask :

Can I have a *sigle non-type* parameter in a *member function* of a *non-templated* class, *and* to be able to specialize it by overloading the nontype parameter?? I want alpha.Handle<1>(data) to do sth with the data, while alpha.Handle<2>(data) to do sth different with the data.

Also I'm worried a bit for my overloading syntax, since I've not seen any example about this.
Feb 3 '08 #3
weaknessforcats
9,207 Expert Mod 8TB
A far as I know, you have to specify a type. The only time you can use soemthing like Handle<1> or Handle<2> is when Handle is a class template and the 1 and 2 are specifed as long or size_t. Like you showed wioth your Stack example.
Feb 3 '08 #4
esrever
10
Oh well I have to compromise then. :( Thanks again!
Feb 3 '08 #5
fual
28
I think that you can do this, presumably you need a pretty new compiler (works on gcc 4.1.2), but the syntax is a little unexpected: http://publib.boulder.ibm.com/infoce...ialization.htm

Take a look at the very bottom of the page, the section entitled "Explicit specialization of members of class templates" The member function needs to be specialised OUTSIDE of the class definition, so your code becomes:

Expand|Select|Wrap|Line Numbers
  1. class A
  2. {
  3. template <int i> void Handle( void * p) { }
  4. };
  5.  
  6. template <> void A::Handle<4>( void * p){ }
  7.  
  8.  
Feb 28 '08 #6
weaknessforcats
9,207 Expert Mod 8TB
"Explicit specialization of members of class templates" The member function needs to be specialised OUTSIDE of the class definition, so your code becomes:
Please read up on bound vs unbound template specializations.
Feb 29 '08 #7
fual
28
Please read up on bound vs unbound template specializations.
Perhaps you have a good link and could summarise? Are you just talking about this: http://www.glenmccl.com/tip_027.htm
Feb 29 '08 #8

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by Dave Theese | last post: by
12 posts views Thread by Simon | last post: by
6 posts views Thread by jesse | last post: by
6 posts views Thread by Matt Taylor | last post: by
3 posts views Thread by Amit | last post: by
7 posts views Thread by Jon Slaughter | last post: by
2 posts views Thread by Mr.Tickle | last post: by
9 posts views Thread by Jerome Durand | last post: by
3 posts views Thread by gieforce | last post: by
reply views Thread by theflame83 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.