Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concept TS check ignores private access modifier

I want to write a concept Indexable meaning that a sequence either has begin/end that return RandomAccessIterator, or operator[] is defined and returns a value of non void type.

I used ideas from Stroustrup's article for the Sequence concept and augmented it with:

template <class T>
concept bool Indexable = Sequence<T> || requires(T t, size_t n) {
    { t[n] } -> NotVoid;
};

It works on most cases but fails on the following:

struct Bad {
    std::vector<int> nums;

private:
    int& operator[](size_t ind) {
        return nums[ind];
    }
};

static_assert(!Indexable<Bad>, "fail");

For some reason my concept ignores the fact that operator[] is defined private and returns true. What am I missing?

like image 980
magom001 Avatar asked Feb 25 '20 10:02

magom001


People also ask

What are the types of access modifiers not supported by TypeScript?

Typescript support four types of access modifiers: public, private, protected and read-only.

How do I access private properties in TypeScript?

In TypeScript there are two ways to do this. The first option is to cast the object to any . The problem with this option is that you loose type safety and intellisense autocompletion. The second option is the intentional escape hatch.

What is private access modifier?

The private modifier specifies that the member can only be accessed in its own class. The protected modifier specifies that the member can only be accessed within its own package (as with package-private) and, in addition, by a subclass of its class in another package.

What is the default access modifier in TypeScript?

TypeScript has two access modifiers – public and private. By default the members are public but you can explicitly add a public or private modifier to them.


1 Answers

This is GCC bug #67225 "Expression constraint with a constrained result turns off access checking", which will be fixed in GCC10.

like image 143
Casey Avatar answered Oct 13 '22 03:10

Casey