如何在 C++ 中为模板函数实例创建快捷方式?
How to make a shortcut for a template function instance in c++?
我想在我的模块(cpp 文件)中使用 boost::math::binomial_coefficient<double>
。但是我不想每次都写那么多杂音,我想给它起一个简单的名字 binom
。但是我很难找到一个合适的方法。
#define
肯定可以,但建议避免使用宏。
将它包装到我自己的函数中也是一种选择,但它很丑陋,污染了 header,有点违背了目的。
我希望我可以使用 using
创建快捷方式,但它无法编译:
using binom = boost::math::binomial_coefficient<double>;
// -> error: expected a type
我知道我可以使用 typedef
为模板类型创建别名 (类),但我不知道如何将其用于函数。
我也尝试过将函数分配给变量,但我收到警告:
auto binom = boost::math::binomial_coefficient<double>;
// -> warning: no previous extern declaration for non-static variable 'binom'
而不是 auto
,使用 const auto
或使用最近的编译器尝试 static constexpr auto
或 inline constexpr auto
。
变量仍然不好,因为它们删除了重载、默认参数,并可能导致不必要的间接寻址。
很遗憾,据我所知,目前还没有好的方法
using binom = boost::math::binomial_coefficient;
差不多了,但不包括<double>
我想在我的模块(cpp 文件)中使用 boost::math::binomial_coefficient<double>
。但是我不想每次都写那么多杂音,我想给它起一个简单的名字 binom
。但是我很难找到一个合适的方法。
#define
肯定可以,但建议避免使用宏。
将它包装到我自己的函数中也是一种选择,但它很丑陋,污染了 header,有点违背了目的。
我希望我可以使用 using
创建快捷方式,但它无法编译:
using binom = boost::math::binomial_coefficient<double>;
// -> error: expected a type
我知道我可以使用 typedef
为模板类型创建别名 (类),但我不知道如何将其用于函数。
我也尝试过将函数分配给变量,但我收到警告:
auto binom = boost::math::binomial_coefficient<double>;
// -> warning: no previous extern declaration for non-static variable 'binom'
而不是 auto
,使用 const auto
或使用最近的编译器尝试 static constexpr auto
或 inline constexpr auto
。
变量仍然不好,因为它们删除了重载、默认参数,并可能导致不必要的间接寻址。
很遗憾,据我所知,目前还没有好的方法
using binom = boost::math::binomial_coefficient;
差不多了,但不包括<double>