隐式将数字转换为整数const [英] Implicitly convert number to integral const
问题描述
假设我具有以下功能:
template<size_t N>
void foo(std::integral_constant<size_t,N>);
现在要使用它,我这样做:
Right now to use it I do this:
constexpr size_t myNum = 12;
foo(std::integral_constant<size_t,myNum>());
但是我想要一种使用它的方法:
But I would like a way to use it like this:
constexpr size_t myNum = 12;
foo(myNum);
是否可以将数字隐式转换为相应的std::integral_constant
?
Is there any way to implicitly convert a number to the corresponding std::integral_constant
?
推荐答案
恐怕不可能进行真正的隐式转换.但是,您可以使用宏和编译时常量检测(请参阅 https://stackoverflow.com/a/13305072/6846474)来模拟所需的语法以及" constexpr重载".
I'm afraid that true implicit conversion is not possible. However, you can use macros and compile-time constant detection (see https://stackoverflow.com/a/13305072/6846474) to emulate the desired syntax along with "constexpr overloading".
这是我的C ++ 14实现的窍门:
Here is my C++14 implementation of the trick:
#include <iostream>
// Compile-time constant detection for C++11 and newer
template <typename T>
constexpr typename std::remove_reference<T>::type makeprval(T && t)
{
return t;
}
#define is_const(X) noexcept(makeprval(X)) // broken in Clang
//#define is_const(X) __builtin_constant_p(X) // non-standard but works in GCC and Clang
template <bool c>
struct const_var_impl {
template <typename CFn, typename VFn>
static inline auto resolve_branch(CFn cf, VFn vf) {}
};
template <>
struct const_var_impl<true> {
template <typename CFn, typename VFn>
static inline auto resolve_branch(CFn cf, VFn vf) {
return cf();
}
};
template <>
struct const_var_impl<false> {
template <typename CFn, typename VFn>
static inline auto resolve_branch(CFn cf, VFn vf) {
return vf();
}
};
#define const_var_branch(X, F) \
const_var_impl<is_const(X)>::resolve_branch( \
[&]() { \
constexpr auto _x_val = is_const(X) ? X : 0; \
return F(std::integral_constant<decltype(X), _x_val>{}); \
}, \
[&]() { \
return F(X); \
} \
)
template <typename T, T c>
void fn_impl(std::integral_constant<T, c> c_arg) {
std::cout << "Constant " << c_arg << std::endl;
}
template <typename T>
void fn_impl(T v_arg) {
std::cout << "Variable " << v_arg << std::endl;
}
#define fn(X) const_var_branch(X, fn_impl)
int main(void) {
int n = 2;
fn(1); // Prints "Constant 1"
fn(n); // Prints "Variable 2"
return 0;
}
您必须使用宏,因为只有常量文字或constexpr表达式被视为编译时常量.据我所知,无法检测到恒定传播.
You have to use a macro because only a constant literal or a constexpr expression is treated as compile-time constant. Constant propagation cannot be detected as far as I know.
因此,我们有两个fn_impl
的重载.编译时和运行时实现.
So, we have two overloads of fn_impl
. The compile-time and the runtime implementation.
主要思想是使用两个lambda函数,其中一个将根据is_const(X)
的值被调用.在将常量/变量X
显式转换为正确的类型后,每个lambda都会调用两个重载之一.正确的lambda将通过const_var_impl
的模板专门化来调用.
The main idea is to use two lambda functions, one of which will be called depending on the value of is_const(X)
. Each lambda calls one of the two overloads after explicitly converting our constant/variable X
to the correct type. The correct lambda is going to be called via a template specialization of const_var_impl
.
棘手的部分是使此工作正常发生而没有编译器错误.您必须以某种方式从const_var_branch
宏中获取X
并尝试从中创建integral_constant
,如果X
是非恒定的,则不可能.
The tricky part was to make this work without compiler errors. You somehow have to take the X
from the const_var_branch
macro and try to create integral_constant
from it which is not possible if X
is non-constant.
幸运的是,我们可以这样做:
Luckily, we can do this:
constexpr auto _x_val = is_const(X) ? X : 0;
现在代码始终有效.如果X
是常量,我们将获取它的值并使用它实例化integral_constant
.否则,我们最终会得到零,这很好,因为无论如何都不会调用带有编译时实现的lambda.
Now the code is always valid. If X
is constant, we get its value and instantiate integral_constant
with it. Otherwise we end up with zero which is fine, because the lambda with compile-time implementation is not going to be called anyway.
此方法的明显局限性在于,对于我们要重载的每个函数,我们必须创建一个宏并将其用于调用.如果我们要在名称空间中调用方法或函数,这当然是不切实际的.
The obvious limitation of this approach is that for each function we want to overload, we have to create a macro and use that for the calls. This is of course not practical if we wanted to call methods or functions within namespaces.
要解决此问题,我们可以创建一个类似的宏,只包装函数参数:
To solve this issue, we could create a similar macro that wraps the function argument only:
#define var_or_const(X) const_var_branch(X, [](auto && x) {return x;})
此宏返回某个整数类型T
的X
或std::integral_constant<T, X>
的实例.
This macro returns either the X
of some integral type T
or an instance of std::integral_constant<T, X>
.
然后我们将直接在fn_impl
上使用它:
Then we would use it on fn_impl
directly:
fn_impl(var_or_const(n));
但是,这还远没有隐含的.
This is however still far from implicit.
这篇关于隐式将数字转换为整数const的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!