Building libraries with explicit implementations of templates.

Started by
-1 comments, last by Skarsha 17 years, 8 months ago
Hi I am currently building libraries for specific instances of template functions. I want to be able to create a library for a specific implementation of functions using specific types, eg: - class: CMemoryMonitor. - implementation: iostream-logging. - types: TYPE_INTEGER = int, TYPE_ADDRESS = size_t, TYPE_CHARACTER = wchar. Note: There are different implementations which are completely independant of type. The implementations are coded in headers using template arguments. My current method is to declare the function (or class) in a header, then write the body of the function in a implementation-specific header using template<> and #defined types like TYPE_INTEGER. Each build of the library then #defines it's own TYPE_INTEGER etc, so there could be a char and wchar version. Builds currently also use different include paths to select their own version of the #included implementation header. I want to get away from using #define. I thought about using typedef, but that isn't really a very clean solution. Example: [path]/foo.h

// Declaration.
template <typename X>
X foo(X value);

[path]/times_two/foo_implementation.h

// Definition.
template <typename X>
X foo(X value) {
    return X * 2;
}

[path]/int/foo.cpp

#include "foo.h"
#include "foo_implementation.h"

template <>
int foo(int value); // <--- The problem line.
// There is already an implementation of this function. Trouble is, it's not going to get build into the library.

// Possible temporary hack.
void very_ugly_hack() {
    int X;
    foo(X); // Force an implementation into the library.
}


build: - sources = [path]/int/foo.cpp - include directories = [path]; [path]/times_two - output = [path]/lib/times_two/int/libfoo.a Note: The reason I want to have the implementations in libraries is because I want to be able to switch between completely different implementations without requiring changes to the actual code (by #including different libraries). For example, I have a memory monitoring function. It takes the types TYPE_ADDRESS, TYPE_INTEGER and TYPE_CHARACTER. But there is a iostream-logging implementation, a runtime-checking implementation and an ignore implementation. Details: -------- Language: C++ cross-platform / cross-compiler. (Using code::blocks, gcc and ubuntu for development and minor testing). Thanks for all help.

This topic is closed to new replies.

Advertisement