473,503 Members | 1,888 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Advanced Template question

AdrianH
1,251 Recognized Expert Top Contributor
Hi all,

I am working on some specialty template library, trying to write a template class that will not allow a constant type.

I've only come up with the following:
Expand|Select|Wrap|Line Numbers
  1. template <class T>
  2. class X
  3. {
  4.   private:
  5.     T* pObj;
  6.   public:
  7.     X(T* pObj)
  8.     : pObj(pObj)
  9.     {
  10. #ifndef NDEBUG
  11.       *(this->pObj)=*pObj;
  12. #endif
  13.     }
  14. };
  15.  
But this has the drawback that if T's copy constructor is private, this will not work. Is there a way of making T not be a constant. Also, if the writer of class T made the constructor wrong, it could do bad things like cause a memory leak if the copy constructor doesn’t check to see if the object it is about to copy is itself.

Is there a simpler way?


Adrian
Feb 9 '07 #1
4 1225
Motoma
3,237 Recognized Expert Specialist
Very good questions, I'm responding only so I can see the answer.
Feb 9 '07 #2
AdrianH
1,251 Recognized Expert Top Contributor
I figured it out. Just used the idea of the assignment operator, which is similar to memcpy(void *, void const *, size_t). So what was needed was to have a function that was similar to that, as I don't really need to copy anything, I made a dummy function that took a void * and a void const *. This is the result:

Expand|Select|Wrap|Line Numbers
  1. template <class T>
  2. class X
  3. {
  4.   private:
  5.     T* pObj;
  6.     void noConstTTest(void *, void const *)
  7.     {
  8.       // If I don't put something here, compiler warns me that this doesn't do
  9.       // anything.  It still doesn't do anything but it shuts the compiler up. :)
  10.       (void)0;
  11.     }
  12.  
  13.   public:
  14.     X(T* pObj)
  15.     : pObj(pObj)
  16.     {
  17.       noConstTTest(this->pObj, pObj);
  18.     }
  19. };
  20.  
I don't even need the pre-processor if/endif statements as the compiler should be able to optimise the call out.

Sneaky eh? ;)


Adrian
Feb 9 '07 #3
AdrianH
1,251 Recognized Expert Top Contributor
Doh, I didn't even need to make it as complicated as I did. All I want to check is if the type is a constant. Here is simpler the result:

Expand|Select|Wrap|Line Numbers
  1. template <class T>
  2. class X
  3. {
  4.   private:
  5.     T* pObj;
  6.  
  7.   public:
  8.     X(T* pObj)
  9.     : pObj(pObj)
  10.     {
  11.       // Keep T from being constant
  12.       void * noConstTTest;
  13.       noConstTTest = reinterpret_cast<T*>(NULL);
  14.     }
  15. };
  16.  
The way this works is that all pointers upcast to constant void pointers, but do not upcast to void pointers unless the original pointer was not pointing at a constant type.

Interesting eh?


Adrian
Feb 9 '07 #4
Motoma
3,237 Recognized Expert Specialist
Interesting eh?
Adrian
Quite! Thanks for posting back!
Feb 9 '07 #5

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

Similar topics

1
1294
by: David2511 | last post by:
Hello, I need a little help. I try to write the following architecture : an abstract template class A Two classes derived from class A : the classes B and C which are concrete. The class...
2
1102
by: Paul | last post by:
Is there a way to explicitly create a template function T whose T is a template class? I would envision the code to look something like this... template <typename T, typename CT<T> > void...
4
2080
by: st_ev_fe | last post by:
Hi people, I've been doing C for about 7 years now. But I'm new to C++. I've decided that C++'s operator overloading could be very handy. I'm writing something much like auto_ptr, except for...
22
2098
by: macAWM | last post by:
Hi list, First let me explain that my background is in Java and I am quite spoiled to its niceties (read "less ambiguous nature"). Anyway to my problems. 1. I want to write my own library for...
1
2072
by: mosscliffe | last post by:
I wanted to install the SQL SERVER 2005 Express Advanced Services, because I wanted the import / export features of the data manager. I chose the route of deleting the template files and then...
2
2721
by: Alex Bögli | last post by:
Hi I have a rather advanced deployment scenario and wanted to know, if anyone has an idea how to accomplish that with ClickOnce: We are deploying a 3-tier application with a client connecting...
1
2003
AdrianH
by: AdrianH | last post by:
ok, i've got a template that takes a pointer: template<class T> struct deref { T* pObj; deref(T* pObj) : pObj(pObj) {} operator T const & () const { return *pObj; } }; and I have the...
3
1892
by: joe | last post by:
I have written a custom std allocator which follows the example in Stroustrup's book. I'm seeing a behavior I don't understand. My allocate method within the allocator looks like the...
1
1425
by: mbatestblrock | last post by:
I think I have a rather advanced question that I was hoping to find some good help with. I am still pretty new to VBA and I know that doesn't help my situation here. But here is what I am trying to...
0
7205
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7093
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7348
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5592
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5021
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4685
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
397
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.