473,219 Members | 2,137 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,219 software developers and data experts.

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 3980
weaknessforcats
9,208 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,208 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,208 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

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

Similar topics

4
by: Dave Theese | last post by:
Hello all, I'm trying to get a grasp of the difference between specializing a function template and overloading it. The example below has a primary template, a specialization and an overload. ...
12
by: Simon | last post by:
Hi, I'm having a problem with templates and specialisation. I'm using it to overload the same function so it can return different things. I can't see what I'm doing wrong, although my compiler...
6
by: jesse | last post by:
I am frustrated by class specialization. i don't think it helps me a lot. suppose we have template <class T> class Talkative { T& t; public:
6
by: Matt Taylor | last post by:
I'm trying to write an x86 assembler in C++ for use in a debugger. What I'd like do is to use template specialization to prevent invalid combinations from compiling. Thus one could not accidentally...
3
by: Amit | last post by:
Greetings. How does one associate a integer variable to a non-type template parameter ? Is there anyway of doing it in an easy way using partial specialization ? so what I have is something to the...
16
by: WittyGuy | last post by:
Hi, What is the major difference between function overloading and function templates? Thanks! http://www.gotw.ca/resources/clcm.htm for info about ]
7
by: Jon Slaughter | last post by:
#pragma once #include <vector> class empty_class { }; template <int _I, int _J, class _element, class _property> class RDES_T {
2
by: Mr.Tickle | last post by:
So whats the deal here regarding Generics in the 2004 release and templates currently in C++?
9
by: Jerome Durand | last post by:
Hello, I'm trying to write something along the following lines but I cannot get this to compile. template <typename derivedstruct Base { typedef typename derived::valueType valueType;...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.