Skip to content

Allow reinterpret_cast in constant expressions in microsoft mode #12488

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
nico opened this issue Feb 28, 2012 · 4 comments
Closed

Allow reinterpret_cast in constant expressions in microsoft mode #12488

nico opened this issue Feb 28, 2012 · 4 comments
Labels
bugzilla Issues migrated from bugzilla c++ wontfix Issue is real, but we can't or won't fix it. Not invalid

Comments

@nico
Copy link
Contributor

nico commented Feb 28, 2012

Bugzilla Link 12116
Resolution WONTFIX
Resolved on Aug 29, 2018 10:24
Version unspecified
OS All
Blocks llvm/llvm-bugzilla-archive#12477
CC @DougGregor,@efriedma-quic,@tritao,@zygoloid

Extended Description

When compiling ms headers with -nobuiltininc, clang picks up offsetof() from msvc's stddef.h ( http://msdn.microsoft.com/en-us/library/dz4y9b9a(v=vs.80).aspx ). If it's used with chrome's STATIC_ASSERT macro, clang complains about "reinterpret_cast not allowed in constant expressions".

I came up with an offsetof() macro that exhibits the same error:

hummer:clang thakis$ cat test.cc
#define offsetof(s,m)
(long)(void*)&((reinterpret_cast<s*>(0)->m))

template
struct GpuCompileAssert {
};

struct SetToken {
int a, token;
};

GpuCompileAssert<(bool(offsetof(SetToken, token) == 4))> foo;

hummer:clang thakis$ ../../Release+Asserts/bin/clang -c test.cc -fms-extensions -fms-compatibility
here4!
here3!
test.cc:12:18: error: non-type template argument of type 'bool' is not an integral constant expression
GpuCompileAssert<(bool(offsetof(SetToken, token) == 4))> foo;
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
test.cc:12:24: note: cast which performs the conversions of a reinterpret_cast is not allowed in a constant expression
GpuCompileAssert<(bool(offsetof(SetToken, token) == 4))> foo;
^
test.cc:2:3: note: expanded from macro 'offsetof'
(long)(void*)&((reinterpret_cast<s*>(0)->m))
^
1 error generated.

We should allow reinterpret_cast<>()s in constant expressions in microsoft mode.

@efriedma-quic
Copy link
Collaborator

How about "don't use -nobuiltininc" instead? We are never going to support MSVC's stdarg.h, and given that, we really just need to make our builtin includes either work or forward appropriately.

@zygoloid
Copy link
Mannequin

zygoloid mannequin commented Feb 29, 2012

Sorry, I'm not prepared to break the constant expression code this much. Accepting (say) reinterpret_casts of null pointers is far from enough to get this treated as a constant expression. "((S*)0)->m" isn't allowed in a constant expression, because you can't access members of null pointers in a constant expression. And the cast from void* to long isn't allowed in a constant expression either.

However, the expression is foldable. If Eli's suggestion is unworkable for some reason and you really need this code pattern to compile, I think it'd be acceptable to make either -fms-extensions or -fms-compatibility allow constant folding in non-type template arguments.

@nico
Copy link
Contributor Author

nico commented Mar 1, 2012

Ok.

@timurrrr
Copy link
Contributor

mentioned in issue llvm/llvm-bugzilla-archive#12477

@llvmbot llvmbot transferred this issue from llvm/llvm-bugzilla-archive Dec 3, 2021
@Quuxplusone Quuxplusone added the wontfix Issue is real, but we can't or won't fix it. Not invalid label Jan 20, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugzilla Issues migrated from bugzilla c++ wontfix Issue is real, but we can't or won't fix it. Not invalid
Projects
None yet
Development

No branches or pull requests

4 participants