1

I want to define a template class with complex behavior. I have trouble with template specialization. To avoid giving the definition of the member functions to the final user, I went for the classic header declaration and source definition. However, how can the user instantiate the class with his custom objects while he does not have the template class definition ? Hereinafter an example with strong types.

// metatype.h
#ifndef METATYPE_H
#define METATYPE_H

#include <utility>

template<typename Type, typename Parameter>
class MetaType
{
public:
    MetaType(const Type &value);
    Type &get();
private:
    Type value;
};

#endif // METATYPE_H

// metatype.cpp
template<typename Type, typename Parameter>
MetaType<Type, Parameter>::MetaType(const Type &value) : value(value) {
}

template<typename Type, typename Parameter>
Type &MetaType<Type, Parameter>::get(const Type &value) {
    return value;
}

// main.cpp
#include <iostream>
#include "metatype.h"

using Real =MetaType<double, struct RealType>;

int main() {
    Real a(3);
    std::cout << a.get() << std::endl;
    return 0;
}

Problem is I have compiling errors due to missing definition of the template specialization. Is there a way to avoid giving the definition to the final user, especially when a complex behavior of the template class is defined ?

Thank you for your anwsers.

0 Answers0