C
Chris Goller
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
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