Reputation: 60491
Here is my example classes :
template<typename T> class MyClassVector
{
public:
inline const std::vector<T>& data() const
{
return _data;
}
protected:
std::vector<T> _data;
};
template<typename T, unsigned int SIZE> class MyClassArray
{
public:
inline const /* SOMETHING */ data() const
{
return _data; // OR SOMETHING ELSE
}
protected:
T _data[SIZE];
};
My question is : what is the equivalent of the MyClassVector data() function for the MyClassArray class to return a constant reference to the underlying _data container ?
Thank you very much !
Upvotes: 8
Views: 5774
Reputation: 18136
Why not just expose the array as pointer to const instance of T? And also the the size of the array?
template<typename T, std::size_t SIZE>
class MyClassArray {
public:
inline const int* data() const
{
return _data;
}
inline std::size_t size() const
{
return SIZE;
}
protected:
std::size_t _data[SIZE];
};
There is an analogous approach: see std::string
class, c_str()
and size()
member functions.
Upvotes: 0
Reputation: 34714
There are two ways to do this: The direct and the readable:
Direct:
inline T const (& data() const)[SIZE] {
return _data;
}
Readable:
typedef T Data[Size];
inline Data const& data() const {
return _data;
}
Upvotes: 16
Reputation: 208476
The closest syntax would be returning a reference to a an array of const T. The simple way of writting that is by means of typedefs:
template <typename T, unsigned int N>
class MyClassArray {
public:
typedef T array_t[N];
typedef const T carray_t[N];
array_t _data;
carray_t& data() const {
return _data;
}
};
The harder to read way is actually spelling the type in the function declaration:
const T (&foo() const)[N] {
return _data;
}
Upvotes: 3
Reputation: 39511
There's no way to do this typesafely since arrays just decay to a pointer. The equivalent would be returning a const pointer, but then the caller has no way to know the size.
Anyway, there's no reason to use raw arrays anymore. Std::array would solve the problem and be much nicer to work with as well.
Edit, nevermind it is possible to pass references to raw arrays (see above). That doesn't mean you should do it though. std::array
is better.
Upvotes: 0
Reputation: 147054
You can return a const
reference to a SIZE
sized array of T. I have no idea what the syntax for this is, but you can use type traits to produce it. But more relevantly, you are simply poorly duplicating std::array<T, N>
, which can also be found in Boost/TR1, so it's a tad of a waste of time.
#include <type_traits>
class X {
int arr[5];
std::add_reference<std::add_const<int[5]>::type>::type foo() {
return arr;
}
};
This will return a size-aware const reference to your array.
Upvotes: 0