Skip to content
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

Add missing deduction guides and use type_identity_t for allocator extended copy/move ctors #556

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

jbcoe
Copy link
Owner

@jbcoe jbcoe commented Feb 16, 2025

Enable more use of deduction guides.

@jbcoe jbcoe requested a review from Twon as a code owner February 16, 2025 19:22
@jbcoe jbcoe requested review from Ukilele and nbx8 February 16, 2025 19:22
Copy link

codecov bot commented Feb 16, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.60%. Comparing base (840a406) to head (af5477f).

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #556   +/-   ##
=======================================
  Coverage   99.60%   99.60%           
=======================================
  Files          11       11           
  Lines         750      750           
  Branches       76       76           
=======================================
  Hits          747      747           
  Misses          3        3           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@@ -13,6 +13,7 @@ cc_library(
srcs = ["indirect.cc"],
hdrs = ["indirect.h"],
copts = ["-Iexternal/value_types/"],
defines = ["XYZ_HAS_EXTENDED_CONSTRUCTOR_TEMPLATE_ARGUMENT_DEDUCTION"],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the purpose of XYZ_HAS_EXTENDED_CONSTRUCTOR_TEMPLATE_ARGUMENT_DEDUCTION? Does it cause problems for some compilers?
I would expect that all compilers should be able to support these deduction guides, and so you wouldn't need to hide them under a build flag.

Copy link
Owner Author

@jbcoe jbcoe Feb 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to keep extended features as explicit opt-in for now.

We check XYZ_HAS_EXTENDED_CONSTRUCTOR_TEMPLATE_ARGUMENT_DEDUCTION in tests to ensure that appropriate tests are only run when extended features are enabled.

Comment on lines +98 to +110
//
// XYZ_HAS_STD_TYPE_IDENTITY
// The macro is defined when std::type_identity_t<T> is available.
//

#ifdef XYZ_HAS_STD_TYPE_IDENTITY
#error "XYZ_HAS_STD_TYPE_IDENTITY is already defined"
#endif // XYZ_HAS_STD_TYPE_IDENTITY

#if (__cplusplus >= 202002L) || (defined(_MSVC_LANG) && _MSVC_LANG >= 202002L)
#define XYZ_HAS_STD_TYPE_IDENTITY
#endif //(__cplusplus >= 202002L) || (defined(_MSVC_LANG) && _MSVC_LANG >=
// 202002L)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This macro is set but never used. You use type_identity_t below without testing this macro.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's used in tests to disable tests that won't run on older C++ versions.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, but it's not used to guard those features in indirect.h. So for example this just errors out:

clang++ -O2 -std=c++17 -c indirect_test.cc

Theoretically you could support CTAD in C++17 (because CTAD itself was a C++17 feature); but right now you have only your C++14 version (which can't use CTAD) and your C++20 version (which assumes full C++20 support).

I'm figuring all this out as I write, so, forgive me if this is all old news. I guess the README already implies that C++17 isn't a supported target. You can probably at least partially ignore this comment, then.

I'm still confused why XYZ_HAS_EXTENDED_CONSTRUCTOR_TEMPLATE_ARGUMENT_DEDUCTION is handled/set/tested in a different way from XYZ_HAS_TEMPLATE_ARGUMENT_DEDUCTION, though.

Comment on lines +468 to +469
template <typename Alloc, typename Value,
typename std::enable_if_t<!is_indirect_v<Value>, int> = 0>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
template <typename Alloc, typename Value,
typename std::enable_if_t<!is_indirect_v<Value>, int> = 0>
template <typename Alloc, typename Value>

The enable_if isn't needed, since the deduction guide on line 475 is more specific. (Please correct me if I'm wrong; I didn't test this.)

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've taken another look and the enable_if is needed for AppleClang at least.

This issue is possibly related: llvm/llvm-project#57646

Perhaps @Ukilele can comment as he'd had some success implementing deduction guides.

Comment on lines +421 to +422
template <typename Alloc, typename Value,
typename std::enable_if_t<!is_polymorphic_v<Value>, int> = 0>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
template <typename Alloc, typename Value,
typename std::enable_if_t<!is_polymorphic_v<Value>, int> = 0>
template <typename Alloc, typename Value>

Same here: the deduction guide on line 427 is more specific so the enable_if isn't needed.

And a big bonus here: you can get rid of your xyz::is_polymorphic_v which is confusingly dissimilar to std::is_polymorphic_v.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

std::is_polymorphic_v<std::polymorphic<T>> evaluating to false is a pretty compelling call for a rename. A new issue shall be raised.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI, std::is_array_v<std::array<int, 10>> and std::is_function_v<std::function<int()>> are both false too. (My C++ Pub Quiz research is finally paying off! ;)) I think it's a bonus to be able to eliminate xyz::is_polymorphic, but if it's really indispensable, then I don't think you should worry about changing the name.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants