Created
October 9, 2016 05:36
-
-
Save Loki-Astari/fe99d3dc95cd9f8ffdcd387308ca4ae7 to your computer and use it in GitHub Desktop.
This file contains 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 <numeric> | |
template <typename T> | |
bool parse(const char* fromstr, T& dest) //T* still can be const T*, since cv is preserved | |
{ | |
//parse and return 1 if succeeded | |
dest = T(); | |
return 1; | |
} | |
template<typename T> | |
int parser(char const*& format, char const* input, T& value) | |
{ | |
#if 0 | |
if (/*T doesn't match the format*/) | |
{ | |
throw bad_format_t{}; | |
} | |
#endif | |
return parse(input, value); //<- tries to write into const object | |
} | |
template<typename... Args> | |
int tscanf(char const* format, char const* value, Args&... args) | |
{ | |
auto count = {parser(format, value, args)...}; //<- lvalue refs again, cv preserved | |
return std::accumulate(std::begin(count), std::end(count), 0); | |
} | |
int main() | |
{ | |
int test = 5; | |
tscanf("","",test); | |
//tscanf("","",5); | |
int const test2 = 4; | |
//tscanf("","",test2); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment