using templates as substitutions for #ifdef
From: Chris Goller (goller_at_gmail.com)
Date: 03/08/05
- Next message: Ludovic Brenta: "Re: [OT] Re: Teaching new tricks to an old dog (C++ -->Ada)"
- Previous message: Will Twentyman: "Re: Strange email address"
- Next in thread: Thomas Matthews: "Re: using templates as substitutions for #ifdef"
- Reply: Thomas Matthews: "Re: using templates as substitutions for #ifdef"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 8 Mar 2005 11:08:34 -0800
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
- Next message: Ludovic Brenta: "Re: [OT] Re: Teaching new tricks to an old dog (C++ -->Ada)"
- Previous message: Will Twentyman: "Re: Strange email address"
- Next in thread: Thomas Matthews: "Re: using templates as substitutions for #ifdef"
- Reply: Thomas Matthews: "Re: using templates as substitutions for #ifdef"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|