Last active
March 15, 2022 06:26
-
-
Save cppcooper/969168fd7f48af93f2cc8fc020aa9bcf to your computer and use it in GitHub Desktop.
[C++] cool niche template stuff
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(","))); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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