Skip to content

opIndex not callable using const Slice #81

@krox

Description

@krox

The following code does not compile.

struct Foo
{
	ContiguousSlice!(1, int) bar;

	int get(size_t i) const
	{
		return bar[i];
	}
}

The problem is that .opIndex cannot be called on a const object. There is an easy workaround, namely bar.toConst[i]. In other contexts, the toConst is called automatically (using some alias this construction). But here the compiler sees the non-const opIndex method and gives an error before even trying to use the alias this.

Calling toConst explicitly is easy to do but kinda unintuitive, because it is used here to remove a const attribute, not to add it. More precisely, it is used for the conversion from const Slice!int to Slice!(const int).

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions