Hi,
There's probably a simple way to get rid of this, but I just can't find it.
I have a class with two function templates. One of them calls the other, and the other one is defined with specializations.
The Foo class is defined in 3 files:
Foo.h
Code:
#pragma once
#include <map>
#include <vector>
class Foo
{
public:
Foo();
virtual ~Foo() {}
int bar();
template <typename T> std::vector<int> enum_keys() const;
template <typename T> const std::map<int, T>& get_map() const;
protected:
std::map<int, int> _int_map;
std::map<int, float> _float_map;
};
#include "Foo.hpp"
Foo.hpp
Code:
template <typename T>
std::vector<int> Foo::enum_keys () const
{
std::vector<int> keys;
const std::map<int, T>& mapref = get_map<T>();
typename std::map<int, T>::const_iterator end = mapref.end();
typename std::map<int, T>::const_iterator it = mapref.begin();
for (; it != end; ++it)
{
keys.push_back(it->first);
}
return keys;
}
template <>
const std::map<int, int>& Foo::get_map<int>() const
{
return _int_map;
}
template <>
const std::map<int, float>& Foo::get_map<float>() const
{
return _float_map;
}
Foo.cpp
Code:
#include "Foo.h"
Foo::Foo()
{
}
int Foo::bar()
{
const std::map<int, float>& floatmap = get_map<float>();
return floatmap.begin()->first;
}
and here is my main:
Code:
#include "Foo.h"
int main(int argc, char** argv)
{
Foo foo;
return 0;
}
The linker gives me a duplicate symbol error I can't get rid of:
Code:
duplicate symbol __gnu_debug_def::map<int, int, std::less<int>, std::allocator<std::pair<int const, int> > >
const& Foo::get_map<int>() const in Foo.o and main.o
I believe there is a way to actually tell the compiler which templates to generate, but I haven't found the way yet...
Anyone can help me on this ?