Skip to content

Value types *can* still be constructed in an invalid state #1786

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Saalvage opened this issue Oct 20, 2023 · 2 comments
Open

Value types *can* still be constructed in an invalid state #1786

Saalvage opened this issue Oct 20, 2023 · 2 comments

Comments

@Saalvage
Copy link
Contributor

Even following #1783, the following value type:

CS_VALUE_TYPE class DLL_API ValueType {
public:
    std::string string_member;
};

can be constructed in an invalid state, e.g. by using ValueType v = default;

Following v.string_member = "..."; resulting reads will result in garbage being read.

A remedy could be a variable to track whether the object was initialized and if not initializing it whenever any operation is called on it.

@tritao
Copy link
Collaborator

tritao commented Oct 20, 2023

Is there no way to prevent default initialization?

Bloating the code for something the compiler could be doing seems annoying.

@Saalvage
Copy link
Contributor Author

I'm fairly certain there is not. C# structs being zero-initialized in a number of cases seems deeply ingrained in the language. I also think this is a bad design choice.

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

No branches or pull requests

2 participants