Skip to content

[SUGGESTION] Already constructed object getting constructed again as we pass it as out parameter ? why ? #716

Closed

Description

@jituderaps

AFAIK, the motive of out parameter is to accept an object(i.e. not constructed yet) and construct it, but what if we pass an already constructed object as out argument, it will get constructed / assigned again and that is inefficient.
for example :
demo of similar problem to understand the construction of object: https://cpp2.godbolt.org/z/zhYo997ad

fun: (out x: int) = x = 5;

main: () -> i32 = {
    v1: int = 9;
    // v2: int;
    
    fun(out v1);       // it should be illegal because it's inefficient (doesn't define the meaning of out parameter)
    // fun(out v2);      // works fine
}

Expected result : compilation error
Actual result/error : running fine

Activity

  1. JohelEGP commented on Oct 2, 2023

    @JohelEGP
    Contributor

    That's like suggesting to ban v1 = 10 because v1 is already constructed.
    = on an initialized variable already behaves like an out argument.
    For more, see https://github.com/hsutter/cppfront/wiki/Cpp2:-operator=,-this-&-that#misc-notes.

  2. Repository owner locked and limited conversation to collaborators on Oct 8, 2023
  3. converted this issue into a discussion #736 on Oct 8, 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