473,794 Members | 3,056 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

using templates as substitutions for #ifdef

Imagine a cross platform library. A particular object wraps up
operating system specific functionality and presents it in a uniform
manner.

Generally, people use ifdefs to change the functionality of a
particular piece of code to match that of the native operating system
(#ifdef _WINDOWS, etc)

I'm wondering if it is possible to use templates to accomplish this
same functionality.

Basically, there might be something like:

enum os_type
{
LINUX,
SOLARIS,
WINDOWS
};

template <os_type os>
class os_wrapper
{
void os_specific_fun ction();
};

template<>
class os_wrapper<WIND OWS>
{
void os_specific_fun ction()
{
windows_specifi c_function();
}
};

template<>
class os_wrapper<SOLA RIS>
{
void os_specific_fun ction()
{
solaris_specifi c_function();
}

};

This way the compiler wouldn't need to compile the class unless it was
instantiated with an operating system as a template argument.

However, the code above doesn't seem to work at least in g++.

So, my question is it possible with some template kung fu to wrap OS
specific functions so that the compiler will compile them ONLY if they
have been instantiated and ignore the code otherwise?

Chris

Jul 23 '05 #1
4 3781
Chris Goller wrote:
Imagine a cross platform library. A particular object wraps up
operating system specific functionality and presents it in a uniform
manner.

Generally, people use ifdefs to change the functionality of a
particular piece of code to match that of the native operating system
(#ifdef _WINDOWS, etc)

I'm wondering if it is possible to use templates to accomplish this
same functionality.

Basically, there might be something like:

enum os_type
{
LINUX,
SOLARIS,
WINDOWS
};

template <os_type os>
class os_wrapper
{
void os_specific_fun ction();
};

template<>
class os_wrapper<WIND OWS>
{
void os_specific_fun ction()
{
windows_specifi c_function();
}
};

template<>
class os_wrapper<SOLA RIS>
{
void os_specific_fun ction()
{
solaris_specifi c_function();
}

};

This way the compiler wouldn't need to compile the class unless it was
instantiated with an operating system as a template argument.

However, the code above doesn't seem to work at least in g++.

So, my question is it possible with some template kung fu to wrap OS
specific functions so that the compiler will compile them ONLY if they
have been instantiated and ignore the code otherwise?

Chris


That is one method. I prefer having files containing OS specific
implementations of functions, and letting the linker handle it all.
The build script would choose which modules to link together.

This helps push the OS specifics to their lowest level.

--
Thomas Matthews
Jul 23 '05 #2
> That is one method. I prefer having files containing OS specific
implementations of functions, and letting the linker handle it all.
The build script would choose which modules to link together.
This is of course the traditional way of handling things. However,
there are numerous benefits to letting the compiler rather than the
linker know what is going on. Take the following example.

Many libraries do mostly the same thing for all platforms. In fact,
this is good design when developing on multiple platforms. They try to
limit the OS-specific code to a few basic functions. Now, let's say
I've developed a library that works on all most major operating
systems. However, a user decides that they want to use this library on
a 37-bit machine that isn't supported. Lets say that modifying the
library itself would require only a few changes in one or two functions
to make it work. The rest of the library would work just fine
unmodified.

The problem is that the user would have to make their own local copy of
the library with just their changes, and change the build the process
to accomodate their new OS. This of course now means that all updates
to the library will require the user to reinsert his changes and build
process changes.

However, if this library uses templates to specify the OS then the user
can use *template specialization* to implement changes to just the
functions he needs to modify. No changes to the build process are
needed.
This helps push the OS specifics to their lowest level.


No, it actually pushes them to the highest level. The build process
affects all portions of the program. Why even bother specifying the OS
when the code itself is completely OS independent?

The point of course is not just all this. The point is that I can't
seem to get the method of using templates to work. That is the
question.

Chris

Jul 23 '05 #3
The following works as expected (it displays "solaris specific
function") on gcc 3.4.2 (MinGW, on Win XP):

#include <iostream>

void windows_specifi c_function() {
std::cout << "windows specific function" << std::endl;
};

void solaris_specifi c_function() {
std::cout << "solaris specific function" << std::endl;
};
enum os_type
{
LINUX,
SOLARIS,
WINDOWS
};

template <os_type os>
class os_wrapper
{
public:
void os_specific_fun ction();

};

template<>
class os_wrapper<WIND OWS>
{
public:
void os_specific_fun ction()
{
windows_specifi c_function();
}

};

template<>
class os_wrapper<SOLA RIS>
{
public:
void os_specific_fun ction()
{
solaris_specifi c_function();
}

};

const os_type operating_syste m = SOLARIS;

int main () {

os_wrapper<oper ating_system> os;

os.os_specific_ function();

return 0;
}
- Jordan

Jul 23 '05 #4
oops, I've just realised that my example doesn't really mean much:
- You're still going to need conditional compilation for #includes (I
don't think you'll be getting around that with template voodoo though
:)
- You'll still get compilation errors because of missing function
declarations for os specific functions (which you could get around by
providing declarations, but that creates far more work that this is all
worth)...

- Jordan

Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
3114
by: Tony Eva | last post by:
The spinbox widget in Tk 8.4 has an option to allow a command to be specified that is invoked when either the up or down buttons of the spinbox are pressed, as in: ... self.sb = Tkinter.Spinbox( self, command = self.sbcmd ) ... def sbcmd( self, *args ): print args
6
2237
by: vch | last post by:
When defining templates, can I cound on the compiler to parse only those templates that are actually used? For example, in the following definitions: <code> template <class T> struct Conv {
5
2018
by: PengYu.UT | last post by:
Hi, I heard that debug some C++ templates is very difficult. I'm wondering whether it is possible to compile C++ program with templates to pure C or C++ program without templates? Best wishes, Peng
2
1517
by: praveenkojha | last post by:
Hi, I am novice in C++ and am more of a C# guy. I have a third party C++ code which I want to create and use as a managed assembly. I have created a .NET win32 application and have copied this C++ code here and made changes according to my needs. However I am always getting this linker error when compiling. I looked a lot n the web and followed all instructions so far of adding msvcrt.lib, glu32.lib, opengl32.lib to the project...
4
1126
by: Jamiil | last post by:
I have a class which only purpose is to provide services to a variety of classes in other files. The 'manipulator' class is aware of the other classes only because the header files have been include in its header file. However, there are times when some of the other classes are not and will not be dealt with, thus the need to include the header files does not arrive. To handle this, I have used compiler preprocessors to prevent the...
4
1563
by: Tomás | last post by:
I'm writing code at the moment which I intend to be 100% portable and well-defined in line with the current C++ Standard. I like the "export" feature, whereby I can put template functions in source files (where I feel they belong -- unless I intend them to be inline, of course). I realise, however, that the majority of compilers are defective in that they do not implement the "export" feature.
8
2177
by: abhishek | last post by:
>>a,b=3,4 7 Now I want to evaluate y by substituting for the evaluated value of x. eval(y) will try to add "a+b" to 3 and return an error. I could do this, 10 but this becomes unwieldy if I have and so on, because the replacements have to be done in exactly the
1
1764
by: titan.nyquist | last post by:
"At the implementation level, the primary difference is that C# generic type substitutions are performed at runtime and generic type information is thereby preserved for instantiated objects." - http://msdn2.microsoft.com/en-us/library/c6cyy67b.aspx Wonder if someone could elaborate this difference to me. I understand performing substitutions at runtime is slow. But, it implies in c++ templates, the type information is not preserved...
104
4624
by: JohnQ | last post by:
Well apparently not since one can step thru template code with a debugger. But if I was willing to make the concession on debugging, templates would be strictly a precompiler thing? I have a feeling the answer I'm going to get back will be "no, because templates have taken on a life of their own since their original conception and now also affect compiler implementation" (read: not good, IMO. John
0
10435
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...
0
10213
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9037
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...
0
6779
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
5436
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
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2920
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.