Skip to content

Free operator overload #4708

Open
Open

Description

If you want to allow "class + x" and "x + class" you need to have free operator overloading, is this possible or is there another way around it? If not, could this be a future feature?

template<typename T, uint32_t N>
class array
{
    T mArr[N];
    void set(const T value) {
        [unroll] for(uint i = 0 ; i < N; i++) { mArr[i] = value; }
    }
    float operator[](const uint32_t pos)
    { return mArr[pos]; }
};

template<typename T, uint32_t N>
array<T,N> operator+ (array<T,N> lhs, T rhs) {
    array<T,N> arr;
    [unroll] for(uint i = 0 ; i < N; i++) { arr.mArr[i] = lhs.mArr[i] + rhs; }  
    return arr;
}
template<typename T, uint32_t N>
array<T,N> operator+ (T lhs, array<T,N> rhs) {
    array<T,N> arr;
    [unroll] for(uint i = 0 ; i < N; i++) { arr.mArr[i] = lhs + rhs.mArr[i]; }  
    return arr;
}

[numthreads(1, 1, 1)]
void main( uint3 DTid : SV_DispatchThreadID )
{
    array<float, 3> arr1;
    array<float, 3> arr2;
    arr1.set(2.0);
    arr2.set(2.0);
    array<float, 3> result = arr1 + 2.0f;
}

Activity

  1. added
    hlsl-next
    and removed
    needs-triage
    on Jun 30, 2023
  2. llvm-beanz commented on Jun 30, 2023

    @llvm-beanz
    Collaborator

    This language feature proposal is tracked in our spec repo as 0008 Non-member Operator Overloading.

    While we haven't finalized on the features for HLSL 202x, this one is almost certainly going to make the cut.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    hlsl-next

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions