You need to know the size oh your string, and the only way to achieve that is to have a parameter pack to use sizeof...
on. You should be able to achieve what you want with a variadic template operator""
:
#include <cstdint>
#include <type_traits>
template<char... String>
auto operator "" _b()
-> typename std::conditional<sizeof...(String) <= 8,
uint8_t,
typename std::conditional<sizeof...(String) <= 16,
uint16_t,
uint32_t
>::type
>::type
{
// Do whatever you want here
}
And here is a test case:
int main()
{
auto a = 10000001_b;
auto b = 100000001_b;
std::cout << std::boolalpha;
std::cout << std::is_same<decltype(a), uint8_t>::value << "\n"; // true
std::cout << std::is_same<decltype(b), uint16_t>::value << "\n"; // true
}
Unfortunately, that solution can't handle the digit separator. Moreover, the std::conditional
machinery is quite ugly. You could probably work something better with boost::mpl::vector
, boost::mpl::at
and some arithmetic operations.