#include <iostream>
#include <string>
#include <type_traits>
#include <vector>
using String = std::string;
template<class T>
class IEFIAttribute {
public:
using value_type = T; // add this to be able to query it later
IEFIAttribute(const String& Key, const T& Value) :
key(Key), // prefer using the member initializer list
value(Value)
{}
// instead of copies, return const references
String const& getKey() const {
return key;
};
T const& getValue() const {
return value;
}
private:
String key;
T value;
};
У вас есть много возможностей сделать специальную обработку для определенных типов.
Шаблон, использующий constexpr, если :
template<typename T>
T special_1(const std::vector<IEFIAttribute<T>>& v, size_t idx) {
if constexpr(std::is_same_v<T, String>) {
std::cout << "special_1() String handler\n";
} else if constexpr(std::is_same_v<T, int>) {
std::cout << "special_1() int handler\n";
} else {
std::cout << "special_1() generic handler\n";
}
return v[idx].getValue();
}
Шаблон со специализациями:
template<typename T>
T special_2(const std::vector<IEFIAttribute<T>>& v, size_t idx) {
std::cout << "special_2() generic handler\n";
return v[idx].getValue();
}
template<>
String special_2(const std::vector<IEFIAttribute<String>>& v, size_t idx) {
std::cout << "special_2() String handler\n";
return v[idx].getValue();
}
template<>
int special_2(const std::vector<IEFIAttribute<int>>& v, size_t idx) {
std::cout << "special_2() int handler\n";
return v[idx].getValue();
}
Или используя добавленный value_type
для выполнения запросов:
int main() {
std::vector<IEFIAttribute<String>> v1{{"testkey", "testvalue"}};
std::vector<IEFIAttribute<int>> v2{{"testkey", 10}};
// decltype(v1)::value_type is the vectors value_type
// and the second value_type is the one added to your class
if(std::is_same_v<decltype(v1)::value_type::value_type, String>) {
std::cout << "is String\n";
} else {
std::cout << "is not String\n";
}
if(std::is_same_v<decltype(v2)::value_type::value_type, int>) {
std::cout << "is int\n";
} else {
std::cout << "is not int\n";
}
std::cout << special_1(v1, 0) << "\n";
std::cout << special_1(v2, 0) << "\n";
std::cout << special_2(v1, 0) << "\n";
std::cout << special_2(v2, 0) << "\n";
}