Uploaded by Gaurav Vasishtha

TagDispatch

advertisement
#include <iostream>
#include <utility>
#include <set>
#include <type_traits> using namespace std;
set<string> names;
string nameFromIdx(int i)
{
return "ok";
}
template<typename T>
void logAndAdd(T &&name);
template<typename T>
void logAndAddImpl(T &&name, std::false_type)
{
names.emplace(std::forward<T>(name));
}
void logAndAddImpl(int idx, std::true_type)
{
logAndAdd(nameFromIdx(idx));
}
template<typename T>
void logAndAdd(T &&name)
{
logAndAddImpl(std::forward<T>(name), std::bool_constant<std::is_integral_v<std::remove_reference_t<T>>>{});
}
int main() {
logAndAdd("Here");
logAndAdd(5);
logAndAdd("really"); for(auto i : names)
cout<<i<<" ";
cout<<endl;
return 0;
}
Download