Компиляция и компоновка моего единственного файла, определяющего пользовательский литерал, не удалась.Файл состоит из пользовательского литерального определения (operator""
), а до этого существует класс шаблона, преобразующий цифры (typename ... Chars
) в число типа NumberT
:
#include <cstdint>
#include <chrono>
#include <limits>
using Du = std::chrono::duration<uint16_t>;
template <typename NumberT, size_t Depth, char ... String>
struct _StringToNumber;
template <typename NumberT, size_t Depth, char Head, char ... Tail>
struct _StringToNumber<NumberT, Depth, Head, Tail ...> {
static_assert('0' <= Head <= '9', "unsupported character in unsigned number literal");
using next = _StringToNumber<NumberT, Depth+1, Tail ...>;
const static size_t total_depth = next::total_depth;
const static NumberT order_value = (total_depth-Depth-1)*(Head - '0');
static_assert(std::numeric_limits<NumberT>::max() - next::value >= order_value, "literal does not fit the underlying type");
const static NumberT value = order_value + next::value;
};
template <typename NumberT, size_t Depth>
struct _StringToNumber<NumberT, Depth> {
const static size_t total_depth = Depth;
const static NumberT value = 0;
};
template <typename NumberT, char ... Chars>
using StringToNumber = _StringToNumber<NumberT, 0, Chars ...>;
template <char ... Chars>
Du operator "" _du () { // my custom literal
return Du(StringToNumber<Du::rep, Chars ...>::value);
}
int main() {
1_du; // Undefined symbols for architecture x86_64: "_StringToNumber<unsigned short, 0ul, (char)49>::value"
StringToNumber<uint16_t, '2'>::value; // apparently works
return 0;
}
Ошибка компоновщика:
Undefined symbols for architecture x86_64:
"_StringToNumber<unsigned short, 0ul, (char)49>::value", referenced from:
std::__1::chrono::duration<unsigned short, std::__1::ratio<1l, 1l> > operator"" _du<(char)49>() in scratch_1-71f359.o
ld: symbol(s) not found for architecture x86_64
странно, я не получаю ошибку, если я заменяю Du и Du :: rep, например, uint16_t.
команда g++ -std=c++17 thefile.cpp
$ g++ --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 10.0.0 (clang-1000.11.45.5)
Target: x86_64-apple-darwin18.0.0
Thread model: posix