Skip to content

Conversation

@NBickford-NV
Copy link
Contributor

@NBickford-NV NBickford-NV commented Jan 14, 2025

Although Visual Studio 2022 allows function members in a templated class to use requires (and this appears to be permitted by the language, although I haven't dug deeper than CPPReference here), Visual Studio 2019 emits Error C7599, "a trailing requires clause is only allowed on a templated function" when attempting to compile members of linear_memory_resource.

My solution is to make these functions templated as well, with a default template argument equal to the class' template argument. Since templated constructors can't be defaulted, I've added the default implementation there as well.

Thanks!

Summary by CodeRabbit

  • Refactor
    • Updated constructors in linear_memory_resource class to improve compiler compatibility
    • Modified default constructor to explicitly initialize parent member
    • Added template parameter adjustments to resolve MSVC 2019 compilation issues

Although Visual Studio 2022 allows function members in a templated class to use `requires`
(and this appears to be permitted by the language, although I haven't dug deeper than CPPReference here),
Visual Studio 2019 emits Error C7599, "a trailing requires clause is only allowed on a templated function"
when attempting to compile members of `linear_memory_resource`.

My solution is to make these functions templated as well, with a
default template argument equal to the class' template argument.
Since templated constructors can't be defaulted, I've added the
default implementation there as well.

Signed-off-by: Nia Bickford <nbickford@nvidia.com>
@coderabbitai
Copy link

coderabbitai bot commented Jan 14, 2025

Walkthrough

The pull request focuses on modifying the linear_memory_resource class in the allocator.hpp header to improve compiler compatibility, specifically for MSVC 2019. The changes involve adding template clauses to constructors and explicitly defining the default constructor to initialize the m_parent member. These modifications aim to address compiler-specific limitations without changing the core functionality of the memory resource management.

Changes

File Change Summary
include/decodeless/allocator.hpp - Updated default constructor to explicitly initialize m_parent
- Added template parameters and requires clauses to constructors for better MSVC 2019 compatibility

Poem

🐰 A Rabbit's Ode to Compiler Delight

In headers where memory dances free,
MSVC stumbles, but we set it free!
Template magic, a compiler's embrace,
Constructors now leap with elegant grace
Compatibility wins, our code takes flight! 🚀

Finishing Touches

  • 📝 Generate Docstrings (Beta)

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai or @coderabbitai title anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 772bcb7 and e9ddcaf.

📒 Files selected for processing (1)
  • include/decodeless/allocator.hpp (3 hunks)
🔇 Additional comments (5)
include/decodeless/allocator.hpp (5)

91-96: LGTM! Explicit initialization maintains the original behavior.

The explicit initialization of m_parent is correct and necessary since templated constructors cannot be defaulted.


76-78: LGTM! Template workaround is consistently applied.

The template workaround is uniformly applied across all constructors, maintaining consistency and readability.

Also applies to: 99-101, 108-110


61-62: LGTM! Template parameter defaulting is safe.

The dummy template parameter U = ResOrAlloc is correctly defaulted and won't interfere with the existing template machinery.

Also applies to: 77-78, 100-101, 109-110


Line range hint 58-113: LGTM! Changes successfully address MSVC 2019 compilation issues.

The modifications effectively work around the MSVC 2019 limitation regarding trailing requires clauses while maintaining the original functionality. The solution is:

  1. Minimal and focused on the compiler issue
  2. Consistent across all affected constructors
  3. Safe with respect to template instantiation and memory management

Line range hint 62-74: Verify that the changes don't affect memory allocation behavior.

While the changes appear to be purely syntactic, let's verify that no memory-related behaviors have been altered.

Also applies to: 79-87, 102-104, 111-113

✅ Verification successful

Type constraints don't affect runtime memory behavior

The changes only add compile-time type constraints through the requires allocator<ResOrAlloc> clause, with no modifications to the actual memory allocation logic or safety checks. The constructor's runtime behavior remains identical to the original implementation.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for any changes to memory allocation patterns
rg -l "allocate|deallocate|reallocate" > allocation_files.txt
git diff origin/main...HEAD -- $(cat allocation_files.txt) | grep -E "^[\+\-].*\b(allocate|deallocate|reallocate)\b"

Length of output: 350


Script:

#!/bin/bash
# Check implementation and usage of allocate_bytes
ast-grep --pattern 'allocate_bytes<$_>($_,$_)'

# Look for allocation-related concept definitions
ast-grep --pattern 'concept $_ = $_<$_> && requires($_) { $$$; };'

# Check for any memory resource implementations
ast-grep --pattern 'class $_ : public memory_resource { $$$ }'

Length of output: 182

Comment on lines +58 to +61
#if _MSC_VER < 1930
// MSVC 2019 doesn't allow a trailing requires clause on a function of a
// templated class. Adding a dummy template like this avoids error C7599.
template<class U = ResOrAlloc>
Copy link

Choose a reason for hiding this comment

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

🧹 Nitpick (assertive)

LGTM! Consider marking this as a temporary workaround.

The compiler version check and template workaround effectively addresses the MSVC 2019 limitation with trailing requires clauses.

Consider adding a TODO comment to remove this workaround once MSVC 2019 support is no longer needed:

 #if _MSC_VER < 1930
+    // TODO: Remove this workaround once MSVC 2019 support is dropped
     // MSVC 2019 doesn't allow a trailing requires clause on a function of a
     // templated class. Adding a dummy template like this avoids error C7599.
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
#if _MSC_VER < 1930
// MSVC 2019 doesn't allow a trailing requires clause on a function of a
// templated class. Adding a dummy template like this avoids error C7599.
template<class U = ResOrAlloc>
#if _MSC_VER < 1930
// TODO: Remove this workaround once MSVC 2019 support is dropped
// MSVC 2019 doesn't allow a trailing requires clause on a function of a
// templated class. Adding a dummy template like this avoids error C7599.
template<class U = ResOrAlloc>

Copy link

@pknowlesnv pknowlesnv left a comment

Choose a reason for hiding this comment

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

LGTM. Half tempted to try class U = void just because it avoids confusion over U being used/expected to match, but not that important. TY!! :)

@pknowles pknowles merged commit 976a34b into decodeless:main Jan 14, 2025
6 checks passed
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.

3 participants