I made a post about this earlier but that one did not have good explanation and therefore I deleted it, hopefully this one will be better. Right now I have two questions about the following code
#include <vector>
#include <sstream>
#include <iostream>
#include <cstdio>
#include <string>
using std::cerr;
using std::cout;
using std::cin;
using std::endl;
using std::string;
using std::vector;
int main() {
// the tests
vector<string> tests {"1.2 when 102"s, "1.2 1.2 1.2"s};
// format and the storage variables
string format {"%d %4s %d"};
int input_1 {-1};
char char_arr[5];
int input_2 {-1};
for (const auto& str : tests) {
cout << "Number of elements matched : ";
cout << std::sscanf(str.c_str(), format.c_str(), &input_1, char_arr,
&input_2) << endl;
cout << input_1 << endl;
cout << char_arr << endl;
cout << input_1 << endl;
}
return 0;
}
When I compile the code on a Mac with clang (clang-703.0.29) I get the following error
test.cpp:16:41: error: no matching literal operator for call to 'operator""s' with
arguments of types 'const char *' and 'unsigned long', and no matching literal
operator template
I thought user defined string literals were fully implemented in C++14. Why then does this code not compile? I may be doing something very stupid here...
If I run my code after removing the s after the literals then I get the following output
Number of elements matched : 2
1
.2
1
Number of elements matched : 3
1
.2
1
Why is input_2 a 1 in the first case? It should not be matched correctly and if it isn't then why is it a 1 and not a -1?
Also if I want to consider a floating point number as invalid in a sscanf call then which escape character or flag should I put in the format string?