有什么方法可以制作参数化用户定义的文字吗?

cel*_*rel 3 c++ literals c++11

不久之前,我对"参数化"用户定义的文字有一个想法,并想知道在当前的C++标准中是否有任何方法可以做到这一点.

基本上,我们的想法是拥有一个用户定义的文字,其行为可以根据一些参数进行调整.作为一个简单的例子,我选择了一个"定点"文字,它将浮点数转换为整数; 参数是小数位数的精度.

这只是一个练习,因为我不确定这在实际应用中是如何有用的.

我的第一个想法是这样的:

namespace fp_impl {
    constexpr int floor(long double n) {
        return n;
    }

    constexpr int pow10(int exp) {
        return exp == 0 ? 1 : 10 * pow10(exp - 1);
    }

    template<int i>
    constexpr int fixed_point(long double n) {
        return floor(n * pow10(i));
    }

    namespace fp2 {
        constexpr int operator"" _fp (long double n) {
            return fixed_point<2>(n);
        }
    }

    namespace fp4 {
        constexpr int operator"" _fp (long double n) {
            return fixed_point<4>(n);
        }
    }
}

template<int prec> struct fp;
template<> struct fp<2> {
    namespace lit = fp2;
};
template<> struct fp<4> {
    namespace lit = fp4;
};

int main() {
    {
        using namespace fp<2>::lit;
        std::cout << 5.421_fp << std::endl; // should output 542
    }
    {
        using namespace fp<4>::lit;
        std::cout << 5.421_fp << std::endl; // should output 54210
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,它不会编译,因为在类范围内不允许使用名称空间别名.(它也有一个问题,要求你手动定义每个版本operator"" _fp.)所以我决定尝试使用宏:

namespace fp {
    namespace detail {
        constexpr int floor(long double n) {
            return n;
        }

        constexpr int pow10(int exp) {
            return exp == 0 ? 1 : 10 * pow10(exp - 1);
        }

        template<int i>
        constexpr int fixed_point(long double n) {
            return floor(n * pow10(i));
        }
    }
}

#define SPEC(i) \
    namespace fp { \
        namespace precision##i { \
            constexpr int operator"" _fp(long double n) { \
                return fp::detail::fixed_point<i>(n); \
            } \
        } \
    }
SPEC(2); SPEC(4);
#undef SPEC
#define fp_precision(i) namespace fp::precision##i

int main() {
    {
        using fp_precision(2);
        std::cout << 5.421_fp << std::endl;
    }
    {
        using fp_precision(4);
        std::cout << 5.421_fp << std::endl;
    }
}
Run Code Online (Sandbox Code Playgroud)

这有效,但它仍然需要为SPEC()您想要使用的每个精度使用宏.当然,可以使用一些预处理器技巧来为0到100之间的每个值执行此操作,但我想知道是否有更像模板解决方案,其中每个都需要实例化.我有一个模糊的想法,使用在模板类中声明为友元函数的运算符"",但我怀疑它也不起作用.

作为一个注释,我确实尝试过template<int i> constexpr int operator"" _fp(long double n),但似乎这不是一个文字运算符的允许声明.

Joh*_*itb 5

您可以返回operator()(int)从文字运算符重载的类类型.然后你可以写

5.421_fp(2);
Run Code Online (Sandbox Code Playgroud)