473,657 Members | 2,389 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 1232
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
1301
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 A contains a static method called CreateA which allows to
2
1110
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 func(CT<T>& c){} - func would accept template class types. Does anyone know if this is possible?
4
2091
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 my own set of classes. Basically, it's a class that is supposed to be allocated automatically on the stack, and possesess one member which is a pointer to an object. Unlike auto_ptr, my object will actually call a lock or unlock function
22
2118
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 what I consider to be some holes in the standard language. How do I go about writing and compiling this without this stupid error about not having a 'main' function. I don't want a stupid 'main' function. (I'm compiling using gcc 4.0.) I would...
1
2078
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 installing, as my version of SQL Server express was part of the install, with Visual Web Developer 2005 Express Edition and I was not confident of re-installing just SQL Server Express. The install progressed quite nicely until I got the following...
2
2735
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 to web services, which then connect to a database. The tiers are all on different nodes and we have around 1000 client users. We currently deploy the web services with Wise and adjust the database (cluster) manually. The clients should be...
1
2008
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 following 2 code snippits:
3
1898
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 following: (FooAllocator<T>) pointer allocate(size_type cnt, typename std::allocator<void>::const_pointer hint = 0)
1
1434
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 accomplish. I have a relatively simple database. The main points are which I have a Form with a sub form in it. This form is a customer form and the main part of the form has their name address and phone number. The sub form has sales...
0
8413
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8324
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8842
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8513
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7352
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6176
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5642
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4173
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4330
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.