I am trying to make a template method that takes a default value for the template parameter. What I did:
In the class header file:
class MyClass{
public:
MyClass();
~MyClass() = default;
template<class T = Peripheral>
T* getPeripheral(int id);
private:
unordered_map<int, Peripheral*> _peripherals;
}
In the cpp file:
template<class T>
T* MyClass::getPeripheral(int id){
return (T*)_peripherals.at(id);
}
And I want to be able to do the following to avoid casting each time:
SensorType1* sensor = myClass.getPeripheral<SensorType1>(1);
Peripheral* peripheral = myClass.getPeripheral<>(0);
When I run the code above I get
undefined reference to myClass::getPeripheral<SensorType1>(int)
and
undefined reference to myClass::getPeripheral<Peripheral>(int)
I simplified a lot the code just to give you an idea of what I am looking for, the idea is just to avoid doing the cast outside and because it's not always needed. It should be possible in c++17, isn't it?