I've tried this code:
#include <iostream>
using namespace std;
constexpr int operator"" _w(int d) { return d; }
struct Watt {
int d;
Watt(int _d) : d(_d) {}
};
Watt operator "" _watt(int d) { return Watt(d); }
int main() {
Watt w1 = 17_w;
cout << w1.d << endl;
return 0;
}
Compile with clang++ 14, it gives:
<source>:3:29: error: invalid literal operator parameter type 'int', did you mean 'unsigned long long'?
constexpr int operator"" _w(int d) { return d; }
^~~~~
<source>:8:24: error: invalid literal operator parameter type 'int', did you mean 'unsigned long long'?
Watt operator "" _watt(int d) { return Watt(d); }
^~~~~
<source>:11:17: error: no matching literal operator for call to 'operator""_w' with argument of type 'unsigned long long' or 'const char *', and no matching literal operator template
Watt w1 = 17_w;
Just wish to know, whether c++14's literal operator limits to support certain restricted data types? Is int
type feasible here in my code? If yes, how to fix it?