是否可以定义用户定义的字符串文字转换运算符,使其结果的类型取决于其字符串输入的值?
使用用户定义的整数和浮点文字很容易,因为它们允许文字运算符模板,并且文字的实际字符作为模板参数传递给它。例子:
template <char... s> struct silly { using type = int; };
template <char... s> struct silly<'1', s...> { using type = double; };
template <char... s>
typename silly<s...>::type operator"" _silly() { return 0; }
static_assert(std::is_same<int, decltype(4321_silly)>::value, "no luck");
static_assert(std::is_same<double, decltype(1234_silly)>::value, "no luck");
用户定义的字符串文字似乎不存在这样的事情。
是否有另一种方法可以做到这一点,无论是在当前标准中还是计划/讨论的未来修订中?