Provide definitions for std::size_t and std::ptrdiff_t through type deduction#5699
Provide definitions for std::size_t and std::ptrdiff_t through type deduction#5699YexuanXiao wants to merge 1 commit intomicrosoft:mainfrom
Conversation
|
As MSVC has supported E.g. #ifdef __cplusplus
using size_t = decltype(sizeof(0));
using ptrdiff_t = decltype(static_cast<int*>(nullptr) - static_cast<int*>(nullptr));
#else // ^^^ defined(__cplusplus) / !defined(__cplusplus) vvv
typedef __typeof__(sizeof(0)) size_t;
typedef __typeof__((int*)0 - (int*)0) ptrdiff_t;
#endif // ^^^ !defined(__cplusplus) ^^^ |
|
86a5933 to
f54141b
Compare
|
Thanks for filing DevCom-10957789. I strongly believe that if we make a change here, it should be in VCRuntime - the STL shouldn't define these typedefs differently (even if compilers accept them being the same fundamental type). The risk of bizarre fallout is simply too great to be worth the incremental benefit. It would help to note in that suggestion what version of Clang this will appear in. (Clang 22?) I'm going to close this PR without merging, but thanks for looking into this and improving Clang. |
Yes, it's expected in the Clang 22 release. |
I previously added a feature to Clang that allows expressions producing
size_tandptrdiff_tthrough built-in language mechanisms (eg.sizeof) to have the sugared types__size_tand__ptrdiff_t, instead ofunsigned int/long/long longandint/long/long long(llvm/llvm-project#143653). This helps generate more portable diagnostic messages. However, the definitions ofsize_tandptrdiff_tin the STL andvcruntime.hare hardcoded to fixed built-in types, causing this benefit to be lost when arithmetic operations are performed, as Clang does not treatstd::size_tandstd::ptrdiff_tdiffer from othertypedefs when evaluates common sugared types.By using deduction to define
std::size_tandstd::ptrdiff_t, this advantage can be preserved.