Skip to content

[clang][enhancement] operator overload attribute for C #141506

Closed as not planned
Closed as not planned
@olemayu

Description

@olemayu

Simple and elegant. Just like C itself.

typedef struct vec3 {
    float x, y, z;
} vec3;

[[clang::operator(*)]]
inline vec3 v3v3_mul(vec3 lhs, vec3 rhs) {
    return (vec3) { lhs.x * rhs.x, lhs.y * rhs.y, lhs.z * rhs.z };
}

[[clang::operator(*)]]
inline vec3 v3v1_mul(vec3 lhs, float rhs) {
    return (vec3) { lhs.x * rhs, lhs.y * rhs, lhs.z * rhs };
}

// lhs/self expected to be a pointer for assignment operators
[[clang::operator(*=)]]
inline vec3 v3v3_smul(vec3* self, vec3 rhs) {
    self->x *= rhs.x;
    self->y *= rhs.y;
    self->z *= rhs.z;
    return *self;
}

[[clang::operator(*=)]]
inline vec3 v3v1_smul(vec3* self, float rhs) {
    self->x *= rhs;
    self->y *= rhs;
    self->z *= rhs;
    return *self;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:frontendLanguage frontend issues, e.g. anything involving "Sema"enhancementImproving things as opposed to bug fixing, e.g. new or missing featurequestionA question, not bug report. Check out https://llvm.org/docs/GettingInvolved.html instead!

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions