I have a simple class:
#include <variant>
struct RawDataArray{
std::variant<double*, float*> data;
template <typename T>
constexpr bool IsType() const noexcept{
return std::holds_alternative<T*>(data);
}
template <typename T>
T& operator [](const int index){
return std::get<T*>(data)[index];
}
};
int main(){
double* tmpData = new double[3];
tmpData[0] = 1;
tmpData[1] = 2;
tmpData[2] = 3;
RawDataArray rawData;
rawData.data = tmpData;
rawData[0] = 0.0;
}
However, I got:
error C2676: binary '[': 'RawDataArray' does not define this operator or a conversion to a type acceptable to the predefined operator
message : could be 'T &RawDataArray::operator [](const int)'
message : 'T &RawDataArray::operator [](const int)': could not deduce template argument for 'T'
I understand the error, but I don't know, how to write such a method. I thought that using 0.0
or 0.0f
would auto-deduce the T
. I also tried to specify variable double x = 0.0
and use this, with the same error.