Skip to content

[QUESTION] How to make of inout parameters of type const T? #250

@leejy12

Description

@leejy12

I was thinking of how Cpp2 would implement std::min. Its signature looks like this:

template <typename T>
const T& min(const T& a, const T& b);

I came up with this answer (the template syntax is copied from #248 (comment):

min: <T: type> (inout a: const T, inout b: const T) -> forward const T = {
    if a < b {
        return a;
    }
    else {
        return b;
    }
}

https://godbolt.org/z/8Wa315arP

However, this raises some questions about inout and const parameters.

inout x: X means "present me an X I can read and write" and the function must contain a non-const use of X. But functions like std::min can't have write operations to its parameters because they're const reference. So cppfront should not give error diagnostics when there's no write operations to parameters that are inout and const.

I think this slight conflict of intentions could be confusing to beginners learning the language. Unless there's a better way, we could treat this as a special case of using inout and const for functions that MUST take parameters by const reference. I don't think there's many functions that do, I can only think of std::min, std::max, and std::clamp.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions