File tree Expand file tree Collapse file tree 1 file changed +11
-10
lines changed
Expand file tree Collapse file tree 1 file changed +11
-10
lines changed Original file line number Diff line number Diff line change 1515#ifndef STRTAG_H
1616#define STRTAG_H
1717
18- #include < stdexcept>
18+ #include < cstring>
19+ #include < cstdint>
1920#include < string>
21+ #include < type_traits>
2022
21- template <class T = uint64_t >
22- constexpr T qStr2Tag (const char * str)
23+ template <class T = uint64_t , std:: size_t N >
24+ constexpr T qStr2Tag (const char (& str)[N] )
2325{
24- if (strlen (str) != sizeof (T)) {
25- throw std::runtime_error (" Invalid tag length" );
26+ static_assert (std::is_trivially_copyable_v<T>);
27+ static_assert (N - 1 == sizeof (T), " Invalid tag length" );
28+ T value{};
29+ for (std::size_t i = 0 ; i < sizeof (T); ++i) {
30+ value |= T (static_cast <unsigned char >(str[i])) << (i * 8 );
2631 }
27- T tmp;
28- for (uint32_t i = 0 ; i < sizeof (T); i++) {
29- ((char *)&tmp)[i] = str[i];
30- }
31- return tmp;
32+ return value;
3233}
3334
3435template <class T >
You can’t perform that action at this time.
0 commit comments