Skip to content

Instantly share code, notes, and snippets.

@Loki-Astari
Created October 9, 2016 05:36
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 Loki-Astari/fe99d3dc95cd9f8ffdcd387308ca4ae7 to your computer and use it in GitHub Desktop.
Save Loki-Astari/fe99d3dc95cd9f8ffdcd387308ca4ae7 to your computer and use it in GitHub Desktop.
#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