Skip to content

Instantly share code, notes, and snippets.

@9prady9
Created May 17, 2020 09:49
Show Gist options
  • Save 9prady9/1d49c3289b47f84424ba883f5d11c7d3 to your computer and use it in GitHub Desktop.
Save 9prady9/1d49c3289b47f84424ba883f5d11c7d3 to your computer and use it in GitHub Desktop.
Convert type and non-type template arguments to a vector of strings
#include <type_traits>
#include <vector>
#include <string>
#include <algorithm>
#include <iostream>
#include <iterator>
typedef enum {
ADD = 0,
SUB
} Enum;
template<typename InputType>
std::string toString(InputType value) {
return std::to_string(value);
}
template<>
std::string toString(bool p) {
return (p ? "true" : "false");
}
template<>
std::string toString(Enum p) {
const char* retVal = NULL;
#define CASE_STMT(v) \
case v: retVal = #v; break
switch (p) {
CASE_STMT(ADD);
CASE_STMT(SUB);
}
#undef CASE_STMT
return retVal;
}
template<typename T> struct TypeTraits;
template<>
struct TypeTraits<float> {
using type = float;
static const char* getName() { return "float"; }
};
template<typename... InstanceTypes>
struct TemplateArgs {
template<typename... ArgsTypes>
auto operator()(ArgsTypes... args) const {
using std::vector;
using std::string;
using std::begin;
using std::end;
vector<string> typeParams = { TypeTraits<InstanceTypes>::getName()... };
vector<string> nonTypeParams = { toString(args)... };
typeParams.insert(std::end(typeParams),
std::begin(nonTypeParams), std::end(nonTypeParams));
return typeParams;
}
};
int main() {
TemplateArgs<float> types;
for(auto str: types(2, false, ADD)) {
std::cout<< str << std::endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment