[llvm-bugs] [Bug 40831] New: bad code with hand-crafted offsetof
via llvm-bugs
llvm-bugs at lists.llvm.org
Sat Feb 23 10:24:41 PST 2019
https://bugs.llvm.org/show_bug.cgi?id=40831
Bug ID: 40831
Summary: bad code with hand-crafted offsetof
Product: clang
Version: unspecified
Hardware: PC
OS: Linux
Status: NEW
Severity: enhancement
Priority: P
Component: C++
Assignee: unassignedclangbugs at nondot.org
Reporter: stsp2 at yandex.ru
CC: blitzrakete at gmail.com, dgregor at apple.com,
erik.pilkington at gmail.com, llvm-bugs at lists.llvm.org,
richard-llvm at metafoo.co.uk
As offsetof doesn't work with template arguments,
I have to create the one by hands:
---
#include <cstddef>
#include <iostream>
class L {};
template <typename T, L (T::*M)[0]>
struct offset_of {
constexpr operator size_t() const {
return (std::uintptr_t)&(((T*)nullptr)->*M);
}
};
template <typename T, L (T::*M)[0]>
struct B {
char aa[10];
static const int off = offset_of<T, M>();
};
struct A {
char a;
L _mark[0];
B<A, &A::_mark> b;
};
int main()
{
A a;
std::cout << "size " << sizeof(A) << " off " << a.b.off << std::endl;
return 0;
}
---
This example works perfectly with gcc:
size 11 off 1
But behaves badly when compiled with clang:
size 1 off 1
(and sometimes even segfaults, it seems)
Besides fixing the bug, I wonder if it would be
possible to somehow fix offsetof() to allow the
use of templated args? Otherwise people have to
apply the horrible hacks like this.
--
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20190223/84e8109e/attachment.html>
More information about the llvm-bugs
mailing list