środa, 16 kwietnia 2014

Yet another interesting dark corner of C++

Dear C++ experts ;>:

Is following code correct or is it ill-formed?

typedef vector<int> Integers;

Surely, it isn't. What about following?

vector<int> typedef Integers;

Is it well formed? It turns out that it is. I've found this information here. I decided to dig in a little.
It seems that defining multiple type aliases for particular type is fine when the type is same for all aliases. For example, following snippet is not ill-formed:

typedef vector<int> Integers;
vector<int> typedef Integers;

This is suspicious, though. This is nearly the same what is used in Standard Template Library (STL), e.g. in initializer_list:

class initializer_list {   
public:
    typedef _E    value_type;
    typedef const _E&    reference;
    typedef const _E&    const_reference;

Here we can see that initializer_list defines reference and const_reference type aliases, but they actually are of same type. Actually this happened earlier in std::set, where both iterator and const_iterator are constant (otherwise a programmer could interfere on std::set internals).

The question is - in how many C++ places we can expect similar (unfamiliar) syntactical opportunities? Please share your ideas in comments. From my experience I can add the following.

struct S {
    void static const meth(int const) {}
};

The first interesting thing is that the static keyword occurs in quite weird place. The second thing, far more interesting is the const void type. There's no point to have such a type in C++ language. Or is it?



Brak komentarzy:

Prześlij komentarz