我尝试constexpr将 ipv4 地址解析为四位数字,但它不起作用。所以我将代码精简到失败的地方:
#include <cstdint>
#include <stdexcept>
#include <string_view>
#define WORKS 1
static constexpr uint8_t
getIpVal(const std::string_view &sv) noexcept {
size_t pos = 0;
auto len = sv.size();
unsigned long val = 0;
while (sv[pos] >= '0' && sv[pos] <= '9' && pos < len) {
int digit = sv[pos] - '0';
val *= 10;
val += digit;
if (val > UINT8_MAX) {
return 0;
// throw std::invalid_argument(sv.data());
}
++pos;
}
if (pos < len) {
return 0; …Run Code Online (Sandbox Code Playgroud)