You should make Get
function templated and use std::enable_if
like this:
#include <type_traits>
#include <iostream>
#include <string>
template <class T>
class LabeledEntry
{
// ...
public:
template <class U = T>
typename std::enable_if<std::is_same<U, std::string>::value, U>::type
Get()
{
return {"string"};
}
template <class U = T>
typename std::enable_if<!std::is_same<U, std::string>::value, U>::type
Get()
{
return {42};
}
};
int main()
{
LabeledEntry<std::string> sle;
std::cout << sle.Get() << std::endl;
LabeledEntry<int> ile;
std::cout << ile.Get() << std::endl;
return 0;
}