#Subscripting vectors from the opencl vector extension

4 messages · Page 1 of 1 (latest)

hollow schooner
#

I want to overload my wrapper class's subscript operator so I don't need to manually cast it to to it's held type.

template <typename T>
class Value {
private:
    T value;
public:
    Value<T>() {};
    Value<T>(T value) {this->value = value;};
    T getValue() { return value; }
    operator const T() { return getValue(); }
    template <typename K>
    decltype((std::declval<T>()[std::declval<K>()])) operator [](K index) {
        return value[index];
    }
};```
This works for std::vector but not vectors from the extension since subscripting those doesn't return a reference.
pallid jacinthBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

hollow schooner
#

this seems to work

    template <typename K>
    decltype(auto) operator [](K index) {
        if constexpr (__has_attribute(ext_vector_type)) {
            return *((( typename std::remove_reference_t<decltype(std::declval<T>()[std::declval<K>()])> *) &value)+index);
        }
        else {
            return value[index];
        }
    }
``` I'll leave this open for a while to see if anyone has a less ugly solution
hollow schooner
#

!solved