Commutative property a [i] == i [a]

For an inline array of integer type, say

int a[10];
int i = 2;
a[i] = 10;

as an alternative

i[a] = 10;

because

a[i]is a postfix expression, which is *(a+i)or *(i+a)because the commutative property of addition.

I want to achieve this for a user-defined type:

class Dummy
{
//
};

Is it possible? If so, how? If not, why?

EDIT: - I know this is ugly, but after compiling the code: g ++ -dumpversion 4.3.3

#include <stdio.h>
#include<iostream>
#include <string.h>
#include <malloc.h>
using namespace std;

int main()
{
    string ArrayS[10];
    2[ArrayS] = "ADASD" ;
    cout <<  2[ArrayS] << endl;
    return 0;
}
+3
source share
6 answers

, "operator[] - " ( §13.5.5/1), , .

( , -, , . , , .)

, index[ object ] - , operator[].


.

" E1[E2] ( ) *((E1)+(E2))" (§5.2.1) operator+ , . options: "array" , "index" enum.

, "", operator*. GCC . .

Edit: Ah, §13.6/13 5.2.1 , , , T& operator[](std::ptrdiff_t, T*); T& operator[](T*, std::ptrdiff_t);. .

+15

++ . , , . .

#include <memory>
#include <stdlib.h>
#include <stdio.h>

void *aligned_malloc( size_t bytes, size_t alignment ) {
    void *p = malloc( bytes + alignment ), *pa = reinterpret_cast<void*>( reinterpret_cast<size_t>(p) + alignment &- alignment );
    reinterpret_cast<void**>(pa)[-1] = p;
    return pa;
}

void aligned_free( void *pa ) {
    void *p = reinterpret_cast<void**>(pa)[-1];
    free( p );
}

struct SupportReverseIndexer
{
    class IndexerReversal
    {
        static const size_t alignment;
        friend struct SupportReverseIndexer;
        friend class std::auto_ptr<IndexerReversal>;
        struct SupportReverseIndexer* const m_parent;
        IndexerReversal(struct SupportReverseIndexer* parent) : m_parent(parent) {}
        void* operator new(size_t bytes) { return aligned_malloc(bytes, alignment); }
        void operator delete(void* p) { aligned_free(p); }
        static struct SupportReverseIndexer* getParent(IndexerReversal* pThis)
        {
            size_t iThis = reinterpret_cast<size_t>(pThis);
            iThis += alignment >> 1;
            iThis &= ~(alignment  - 1);
            return reinterpret_cast<IndexerReversal*>(iThis)->m_parent;
        }
    public:
        operator size_t() { struct SupportReverseIndexer* const parent = getParent(this); return parent->indexer(this-parent->ir.get()); }
    };

    SupportReverseIndexer() : ir(new IndexerReversal(this)) {}

    operator IndexerReversal*() { return ir.get(); }

private:
    std::auto_ptr<IndexerReversal> ir;
    size_t indexer(size_t index) { printf("Custom operator[] called, index = %i\n", index); return index; }
};

const size_t SupportReverseIndexer::IndexerReversal::alignment = 0x10000 * sizeof(SupportReverseIndexer::IndexerReversal);

int main(void)
{
    SupportReverseIndexer sri;
    int a = sri[2];
    a = 3[sri];
    a = (-5)[sri];

    return 0;
}

, , !!!!!

+3

a [i] = 10;
i [a] = 10;
a [i] , * (a + i) * (i + a), .

. a[i] == i[a] == *(a+i) == *(i+a) , a[i] C . " C". ++ . , a[i] POD C. ++ , [] C.

, . .

+2

. Potatoswatter , - , operator[](int, T), .

, :

struct foo
{
    operator const foo*() const
    {
        return this;
    }
};

int main()
{
    foo f;
    5[f]; // UB
}

? :

5.2.1
, , . " ", . lvalue "T". "T" .56). E1[E2] ( ) *((E1)+(E2)).

E1 , E2 . .

+ *. operator+ , . E1 + E2 , operator* .

, .

+2

Dummy::operator[](const Dummy& ref), . :

#include <iostream>

class dummy
{
public:
    int operator[](const dummy& ref) { return 0; }
};

int main(int argc, char *argv[])
{
    dummy d1, d2;

    std::cout << (d1[d2] == d2[d1] ? "Yes" : "No");
}

, , , , , return 0; , , , , .

? ?

0

Dummy, ​​ , , Dummy .

0

Source: https://habr.com/ru/post/1762517/


All Articles