Skip to content

[SUGGESTION] unsafe_narrow is not a good name for forced upcasts. #510

Description

@MaxSagebaum

If you have e.g. expression templates, then the upcast to the implementing class needs to be done with unsafe_narrow.

Maybe an alias of unsafe_narrow like unsafe_cast, 'unsafe_pointer_cast` or something like that could be introduced.

Example: https://cpp2.godbolt.org/z/jb1MY3Mcq

Exp : <Impl> type = {  
  cast: (this) -> forward Impl = cpp2::unsafe_narrow<Impl>(this&)*; // unsafe_narrow does not really express what this cast is.
}

A : type = {
  this: Exp<A> = ();
  foo: (this) = std::cout << "foo" << std::endl;
}

main: () -> int = {
  a: A = ();
  e: *Exp<A> = a&;

  e*.cast().foo();

  return 0;
}

Activity

  1. JohelEGP commented on Jun 13, 2023

    @JohelEGP
    Contributor
  2. MaxSagebaum commented on Jun 13, 2023

    @MaxSagebaum
    ContributorAuthor

    Yes, that is also possible.

  3. JohelEGP commented on Jun 13, 2023

    @JohelEGP
    Contributor

    By the way,
    I noticed in your other issue (and this one)
    that you mixed up the terms upcast and downcast.
    Then, I tried to find it in the specification for static_cast,
    but it wasn't mentioned, so I just left it unsaid.
    Now, I did find other examples.
    These use "upcast" when converting from derived to base:
    https://eel.is/c++draft/class.cdtor#example-3 and https://eel.is/c++draft/class.cdtor#example-1.

  4. MaxSagebaum commented on Jun 13, 2023

    @MaxSagebaum
    ContributorAuthor

    Thanks for pointing this out. I knew, that this can also be seen the other way round. I will start using it in terms of the type system and not the functionality.

  5. JohelEGP commented on Jun 19, 2023

    @JohelEGP
    Contributor

    cpp2::unsafe_narrow is modeled after gsl::narrow_cast.
    gsl::narrow_cast seems to be modeled after gsl::narrow
    based on its uses in the C++ Core Guidelines.
    Although gsl::narrow_cast<T>(x) is specified as being static_cast<T>(x)
    [ Note: static_cast<T>(x) is illegal Cpp2. -- end note ]
    it's not clear if those should work with non-arithmetic types:
    See isocpp/CppCoreGuidelines#1845 and linked issues (and their linked issues/PRs).

  6. MaxSagebaum commented on Jun 19, 2023

    @MaxSagebaum
    ContributorAuthor

    Ok, cpp2::unsafe_narrow should be equally handled as the gsl one. But there should be additional casts in cpp2.

  7. Repository owner locked and limited conversation to collaborators on Aug 30, 2023
  8. converted this issue into a discussion #630 on Aug 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions