This question is a follow-up question to Why is alloca returning the same address twice?. I've found a way to get a different memory address to both instances by using an array.
vml.h
#pragma once
#include <iostream>
namespace vml {
// Vectors
template <typename in_type, const int in_length>
class vec {
public:
vec(in_type* in_data) {
std::cout << data << std::endl;
std::copy(in_data, in_data + in_length, data);
}
vec() {
data = nullptr;
}
in_type& operator()(int index) const {
_ASSERT(0 <= index && index < in_length);
return data[index];
}
private:
in_type data[in_length];
};
main.cpp
#include <memory>
#include "vml.h"
int main() {
int list[] = { 1,2,3 };
int list2[] = {2,4,6 };
vml::vec<int, 3> a(list);
vml::vec<int, 3> b(list);
a(1) = 3;
return 0;
}
However, when I run the code I get an error
Error C2440 'return': cannot convert from 'const in_type' to 'in_type &'
Since the return value is 'data[index]' this must mean that it is constant, however, I did not define it as a constant so why is this happening?