Skip to content

Instantly share code, notes, and snippets.

@cppcooper
Last active March 15, 2022 06:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cppcooper/969168fd7f48af93f2cc8fc020aa9bcf to your computer and use it in GitHub Desktop.
Save cppcooper/969168fd7f48af93f2cc8fc020aa9bcf to your computer and use it in GitHub Desktop.
[C++] cool niche template stuff
struct delimiter_ctype : std::ctype<char> {
static const mask* make_table(std::string delims) {
// make a copy of the "C" locale table
static std::vector<mask> v(classic_table(), classic_table() + table_size);
for(mask m : v) {
m &= ~space;
}
for(char d : delims) {
v[d] |= space;
}
return &v[0];
}
delimiter_ctype(std::string delims, ::size_t refs = 0)
: ctype(make_table(delims), false, refs) {}
};
void foo() {
std::string line;
std::stringstream ssline;
ssline.imbue(std::locale(ssline.getloc(),new delimiter_ctype(",")));
}
#include <type_traits>
#define DECLARE_HASA(what) \
template<typename T, typename = int> struct has_##what : std::false_type { };\
template<typename T> struct has_##what<T, decltype((void) T::what, 0)> : std::true_type {};
DECLARE_HASA(when) //declares above statement with 'when' replacing 'what'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment