获取元组元素的偏移量
我编写了以下代码来获取元组元素的偏移量
template<size_t Idx,class T>
constexpr size_t tuple_element_offset() {
return static_cast<size_t>(
reinterpret_cast<char*>(&std::get<Idx>(*reinterpret_cast<T*>(0))) - reinterpret_cast<char*>(0));
}
这实际上类似于 offsetof 宏的实现。 它看起来很难看,但在 gcc-4.6 上编译并运行良好,
typedef std::tuple<int,char,long> mytuple;
mytuple var = std::make_tuple(4,'c',1000);
char * ptr = reinterpret_cast<char*>(&var);
long * pt = reinterpret_cast<long*>(ptr+tuple_element_offset<2,mytuple>());
std::cout << *pt << std::endl;
打印“1000”。
我对 constexpr 不太了解,所以我的问题是:
- 它是合法的 c++ 吗?
- 更重要的是,为什么我可以打电话 std::get (非 constexpr) 在 constexpr 函数内?
据我了解 constexpr,编译器被迫评估结果 在编译时对表达式进行修改,因此在实践中不会发生零引用。
I have wrote the following code to get the offset of a tuple element
template<size_t Idx,class T>
constexpr size_t tuple_element_offset() {
return static_cast<size_t>(
reinterpret_cast<char*>(&std::get<Idx>(*reinterpret_cast<T*>(0))) - reinterpret_cast<char*>(0));
}
This is actually similar to the implementation of the offsetof macro.
It looks ugly, but compiles and works fine on gcc-4.6
typedef std::tuple<int,char,long> mytuple;
mytuple var = std::make_tuple(4,'c',1000);
char * ptr = reinterpret_cast<char*>(&var);
long * pt = reinterpret_cast<long*>(ptr+tuple_element_offset<2,mytuple>());
std::cout << *pt << std::endl;
prints "1000".
I don't know too much about constexpr, so my questions are:
- Is it legal c++?
- More important, why I am allowed to call
std::get (which is non constexpr)
inside a constexpr function?
As far as I understand constexpr, the compiler is forced to evaluate the result
of the expression at compile time, so no zero-dereferentiation can occurs in practice.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果“合法”是指“格式良好”,那么,是的。
如果“合法”的意思是“有效并且可以在任何编译器和标准库实现上工作,那么不行,因为
std::tuple
不是 POD。基本上,
constexpr
函数不一定只包含常量表达式。如果您尝试在常量表达式中使用tuple_element_offset()
函数,则会出现编译错误。这个想法是,在某些情况下,函数可能在常量表达式中可用,但在其他情况下则不然,因此不存在
constexpr
函数必须始终在常量表达式中可用的限制(因为有不是这样的限制,也有可能特定的 constexpr 函数可能永远无法在常量表达式中使用,就像您的函数的情况一样)。C++0x 草案有一个很好的例子(来自 5.19/2):
If by "legal" you mean "well-formed," then, yes.
If by "legal" you mean "valid and will work on any compiler and Standard Library implementation, then, no, because
std::tuple
is not POD.Basically, a
constexpr
function doesn't necessarily have to consist of just a constant expression. If you tried to use yourtuple_element_offset()
function in a constant expression, you'd get a compilation error.The idea is that a function might be usable in a constant expression in some circumstances but not in others, so there isn't a restriction that a
constexpr
function must always be usable in a constant expression (since there isn't such a restriction, it's also possible that a particularconstexpr
function might never be usable in a constant expression, as is the case with your function).The C++0x draft has a good example (from 5.19/2):