我写了下面的代码来获取元组元素的偏移量
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,编译器被迫评估结果 表达式在编译时,因此在实践中不会发生零解引用。
最佳答案
Is it legal C++?
如果您所说的“合法”是指“格式良好”,那么,是的。
如果您所说的“合法”是指“有效并且适用于任何编译器和标准库实现,那么,不,因为 std::tuple
不是 POD。
Why I am allowed to call
std::get
(which is notconstexpr
) inside aconstexpr
function?
基本上,constexpr
函数不一定只包含常量表达式。如果您尝试在常量表达式中使用 tuple_element_offset()
函数,则会出现编译错误。
这个想法是一个函数在某些情况下可能在常量表达式中可用,但在其他情况下不可用,因此没有限制 constexpr
函数必须始终在常量表达式中可用(由于没有这样的限制,也有可能特定的 constexpr
函数可能永远无法在常量表达式中使用,就像您的函数一样)。
C++0x 草案有一个很好的例子(来自 5.19/2):
constexpr const int* addr(const int& ir) { return &ir; } // OK
// OK: (const int*)&(const int&)x is an address contant expression
static const int x = 5;
constexpr const int* xp = addr(x);
// Error, initializer for constexpr variable not a constant expression;
// (const int*)&(const int&)5 is not a constant expression because it takes
// the address of a temporary
constexpr const int* tp = addr(5);
关于c++ - 获取元组元素的偏移量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5358684/