[llvm-bugs] [Bug 33708] New: Cast a pointer-to-member template argument. MSVC/GCC will. Clang won't.

via llvm-bugs llvm-bugs at lists.llvm.org
Thu Jul 6 23:16:58 PDT 2017


            Bug ID: 33708
           Summary: Cast a pointer-to-member template argument. MSVC/GCC
                    will. Clang won't.
           Product: libc++
           Version: 4.0
          Hardware: PC
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: All Bugs
          Assignee: unassignedclangbugs at nondot.org
          Reporter: mick.pearson at wildblue.net
                CC: llvm-bugs at lists.llvm.org, mclow.lists at gmail.com

I have some ostensibly C++98 code that because of circular dependencies needs
to cast a pointer-to-member of a class to a pointer-to-char of the same class.

I looked into this, and this is considered a reversible operation and
pointer-to-members are understood to be constants. I've used older versions of
GCC and Visual Studio to do this in template arguments.

I am unsure if casting in a template argument is strictly C++98 at all or not.

However it seems as if this might just be an obscure feature that's yet to be
requested of Clang. I assume Clang will perform this cast in a regular context.
But I did not try.

The C++ standard says pointer-to-member casts only work if the new member type
is of an alignment that is less than the type/member being casted. Of course
the cast can also go in the other direction, but it's undefined-behavior if it
was not previously casted to a lesser-alignment type and then back again.

P.S. My code is using this to assign constants belonging to children in
generated code that creates a representation of an XML schema that is very
close to C++ so that operations are strongly typed and strings are not used to
represent names of elements inside of algorithms that can see the generated
headers. The pointer-to-member constants are actually a pointer to the child
itself (every child has a unique template instantiation) and so the set up is
not something that could ever be done by hand, but this allows the child to
find the parent/container without storing a pointer or offset nor other
workaround. It's an integral feature without which Clang can't be a target
compiler. (XML attributes are the same.)

P.P.S. There is a great complication that the pointer-to-member can't refer to
the member itself. That is gotten around by defining layouts that are
identical. The pointers themselves must be converted to offsets from a
reference pointer (e.g. nullptr) and then negated to access the containers.

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/20170707/3e81f84a/attachment.html>

More information about the llvm-bugs mailing list