Skip to content

Initialisation behaves differently in an unsafe function #51

@Dalzhim

Description

@Dalzhim

The following program is currently ill-formed. I believe it shouldn't be. In order to make incremental adoption easy, adding #feature on safety at the top of a preexisting valid c++ program shouldn't make it ill-formed. This way, I can enable safety in a 20k lines cpp files and gradually start creating safe functions and types without changing everything all at once.

#feature on safety

#include <string>
#include <vector>

int main()
{
    std::vector<std::string> vec;
    vec.push_back(std::string{"A string"}); // Error, `vec` is considered uninitialized
}

Here are some excerpts from the current draft that contradict my intuition that adding #feature on safety on an existing valid C++ program should keep compiling the same:

§ 2.3 Explicit mutation
[…]

struct Obj {
  const int& func() const;   // #1
        int& func();         // #2
};

void func(Obj obj) {
  // In ISO C++, calls overload #2.
  // In Safe C++, calls overload #1.
  obj.func();

In order to solve this contradiction, I would expect the above to hold by adding safe to the func function.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions