473,386 Members | 1,720 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,386 software developers and data experts.

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_function();
};

template<>
class os_wrapper<WINDOWS>
{
void os_specific_function()
{
windows_specific_function();
}
};

template<>
class os_wrapper<SOLARIS>
{
void os_specific_function()
{
solaris_specific_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 3760
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_function();
};

template<>
class os_wrapper<WINDOWS>
{
void os_specific_function()
{
windows_specific_function();
}
};

template<>
class os_wrapper<SOLARIS>
{
void os_specific_function()
{
solaris_specific_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_specific_function() {
std::cout << "windows specific function" << std::endl;
};

void solaris_specific_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_function();

};

template<>
class os_wrapper<WINDOWS>
{
public:
void os_specific_function()
{
windows_specific_function();
}

};

template<>
class os_wrapper<SOLARIS>
{
public:
void os_specific_function()
{
solaris_specific_function();
}

};

const os_type operating_system = SOLARIS;

int main () {

os_wrapper<operating_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
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 =...
6
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
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...
2
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...
4
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...
4
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...
8
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...
1
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." -...
104
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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,...

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.