I have the following code I was writing to test template specialization. Is there a way to have the pointer decay to the type so that I can use template parameter deduction to get my M and N so I can have the rows and cols of the two dimensional array? I know I can vector of vector for a 2d array, but this is an exercise in template specialization. The following code works as is, but see if you uncomment the following and comment out the current T** constructor it doesn't work. As a result the constructor currently hard codes the number of rows to be used for deleting the data_ member, this is not ideal and ideally I would get this parameter from the template deduction. I was wondering if there is a way to make this work in the following code shown below.
//doesn't like this constructor
// template <unsigned int M, unsigned int N>
// Data ( T (&d)[M][N] ): data_(d), rows_(M), cols_(N) {};
#include <iostream>
template <typename T>
class Data
{
public:
Data ( const T& d ): data_(d) {};
Data ( T&& d ): data_(std::move(d)) {};
std::string getType() { return "Plain Data Type"; }
private:
T data_;
};
template <typename T>
class Data<T**>
{
public:
//doesn't like this constructor
// template <unsigned int M, unsigned int N>
// Data ( T (&d)[M][N] ): data_(d), rows_(M), cols_(N) {};
Data ( T** d ): data_(d), rows_(25) {};
~Data() {
for ( unsigned int i = 0; i < rows_; ++i)
{
delete [] data_[i];
}
delete [] data_;
}
std::string getType() { return "Pointer to Pointer Data Type"; }
private:
T** data_;
unsigned int rows_;
unsigned int cols_;
};
template <typename T>
class Data<T*>
{
public:
Data ( T* d ): data_(d) {};
~Data() { delete data_; }
std::string getType() { return "Pointer Data Type"; }
private:
T* data_;
};
int main ( int argc, char *argv[])
{
float f(9.65);
Data<int> d1(f);
std::cout << d1.getType() << std::endl;
int *i = new int(5);
Data<int*> d2(i);
std::cout << d2.getType() << std::endl;
int **j = new int*[25];
for ( int i = 0 ; i < 25; ++i)
j[i] = new int[50];
Data<int**> d3(j);
std::cout << d3.getType() << std::endl;
}
output:
Plain Data Type
Pointer Data Type
Pointer to Pointer Data Type
T** and T[n][m] are not equivalent (and I curse the collegiate professors that refuse to teach this). One is a pointer-to-pointer, the other is an array of T[m] of size n.
You can specialize your 2D array type like this:
template<typename T, size_t N, size_t M>
class Data<T[N][M]>
{
public:
Data(const T(&ar)[N][M])
{
for (size_t i=0;i<N;++i)
std::copy(std::begin(ar[i]), std::end(ar[i]), std::begin(data[i]));
}
std::string getType() { return "2D-Fixed Array Data Type"; }
private:
T data[N][M];
};
And one way to use it is like this:
float fdat[10][20];
Data<decltype(fdat)> d4(fdat);
std::cout << d4.getType() << std::endl;
Output
2D-Fixed Array Data Type
Related
I'm trying to initialize a private member array of a class without using the STL (because it is not supported on the Arduino microcontroller platform I'm using). This means no std::array or std::initializer_list etc.
The following compiles correctly using gcc 5.4.0 and avr-gcc 4.9.2, but that seems to be a bug. Clang throws an error saying error: array initializer must be an initializer list (as expected).
Code
#include <iostream>
#define PRINTFN() std::cout << __PRETTY_FUNCTION__ << std::endl
class Object {
public:
Object(int number) : number(number) { PRINTFN(); }
Object(const Object &o) : number(o.number) { PRINTFN(); }
void print() { std::cout << "The number is " << number << std::endl; }
private:
const int number;
};
template <size_t N>
class ManyObjects {
public:
ManyObjects(const Object(&objects)[N]) : objects(objects) {}
void print() {
for (Object &object : objects)
object.print();
}
private:
Object objects[N];
};
int main() {
ManyObjects<3> many = {{1, 2, 3}};
many.print();
}
Output
Object::Object(int)
Object::Object(int)
Object::Object(int)
Object::Object(const Object&)
Object::Object(const Object&)
Object::Object(const Object&)
The number is 1
The number is 2
The number is 3
What is the proper way to initialize objects? Or is it just not possible with the given constraints?
You can use variadic templates:
In ManyObjects class:
template <typename... _Args>
ManyObjects(_Args&&... arguments) :
objects { arguments... }
{
}
More here
Yes variadic templates work but it is a bit tricky :
template <size_t N>
class ManyObjects {
public:
template<typename T, typename ...Args>
ManyObjects(const T& x, Args&&... args) : objects{x, args...}{}
private:
Object objects[N];
};
int main() {
ManyObjects<3> many{1, 2, 3};
ManyObjects<3> copymany{many};
copymany.print();
}
For any fixed N it can be interpreted as :
template <size_t N=3>
class ManyObjects {
public:
ManyObjects(int x, int y, int z) : objects{x, y, z}{}
...
};
What is at play here :
Object cannot be default initialized due to the definition of the constructor Object(int)
Object assignment operator is implicitly deleted because number is const
Thus any array Object arr[N] must be explicitly initialized using an aggregate initialization.
The only way I think of is to perform extended initialization via the variadic templates.
To prevent matching the copy constructor you can specify the first argument outside the parameter pack. You loose the construction of size 0, which can be enabled with a template specialization.
ManyObjects<0> noneof;
noneof.print();
I ended up following the advice of VTT, and creating my own array wrapper.
I'd love to hear some feedback if there are things that I have to look out for, or possible bugs, etc.
#include <iostream>
class Object {
public:
Object(int number) : number{number} {}
void print() { std::cout << "The number is " << number << std::endl; }
private:
const int number;
};
// -------------------------------------------------------------------------- //
template <class T, size_t N> class ArrayWrapper {
public:
T &operator[](size_t index) { return data[index]; }
const T &operator[](size_t index) const { return data[index]; }
T *begin() { return &data[0]; }
const T *begin() const { return &data[0]; }
T *end() { return &data[N]; }
const T *end() const { return &data[N]; }
T data[N];
};
// -------------------------------------------------------------------------- //
template <size_t N> class ManyObjects {
public:
ManyObjects(const ArrayWrapper<Object, N> &objects, const char *name)
: objects{objects}, name{name} {}
void print() {
std::cout << name << std::endl;
for (auto &object : objects)
object.print();
}
private:
ArrayWrapper<Object, N> objects;
const char *name;
};
// -------------------------------------------------------------------------- //
int main() {
ManyObjects<3> many = {{1, 2, 3}, "Many"};
many.print();
}
hello i want to create simple array class to get value and insert value like (array[0] = 4) and this is my program but i have problem to use [] = in same time for insert
template <typename Param>
class arr
{
private:
int Last_point = 0;
Param Data[];
public:
void& operator[]=(int Element_id, Param v)
{
Data[Element_id] = v;
}
Param& operator[] (int Element_id)
{
return Data[Element_id];
}
};
void main()
{
arr <int> Array;
Array[1] = 555;
cout << "Is(" << to_string(Array[1]) << ")" << endl;
system("pause");
}
is there any operator like ([]=)? or for this, i have to use which methods? also i want to get value if just used []
The syntax you are attempting for the operator[] functions is quite wrong. You need something like:
// Version for non-const objects
Param& operator[](std::size_t i)
{
return Data[i];
}
// Version for const objects
Param const& operator[](std::size_t i) const
{
return Data[i];
}
If you want to support arrays whose sizes are known at compile time, you can use:
template <typename Param, std::size_t N>
class arr
{
private:
Param Data[N];
public:
Param& operator[](std::size_t i)
{
return Data[i];
}
Param const& operator[](std::size_t i) const
{
return Data[i];
}
};
If you want to support arrays whose sizes are known at run time, you can use the following. However, you need to be aware of The Rule of Three and make sure to implement the copy constructor and the copy assignment operator properly.
template <typename Param>
class arr
{
private:
Param* Data;
public:
arr(size_t size) : Data(new Param[size]) {}
~arr() { delete [] Data; }
Param& operator[](std::size_t i)
{
return Data[i];
}
Param const& operator[](std::size_t i) const
{
return Data[i];
}
};
Foo& operator[] is sufficient for reading and writing.
Include the size of the array as a template parameter, or replace Param[] with std::vector<Param>.
Use size_t instead of int for array index types.
You don't need to wrap Array[1] in to_string for output.
Don't use void main! The standard says main must be int.
void& is not valid C++ either.
#include <iostream>
using namespace std;
template <typename Param, size_t Size> class arr {
private:
Param Data[Size];
public:
Param &operator[](size_t Element_id) {
return Data[Element_id];
}
};
int main() {
arr<int, 3> Array;
Array[1] = 555;
cout << "Is(" << Array[1] << ")" << endl;
}
However, all that arr does in my snippet is be a less useful std::array!
Being on a similar condition of Handling different datatypes in a single structure
I have an implementation using union, but as it takes the highest variable type as its memory size, I end up allocating 64-bit even for an 8-bit variable type. Also I was writing several functions to get/set values on a specific data type.
Example:
class CDataType {
public:
void setTBOOL8(bool src) {
m_uAnyData.um_Bool = src;
}
void setTUINT8(uint8_t src) { //also used for FzrteByte
m_uAnyData.um_UInt8 = src;
}
......
bool getTBOOL8() const {
return (m_uAnyData.um_Bool);
}
uint8_t getTUINT8() const { //also used for FzrteByte
return (m_uAnyData.um_UInt8);
}
.......
private:
union uAnyData {
bool um_Bool;
uint8_t um_Byte;
uint16_t um_Word;
uint32_t um_DWord;
uint64_t um_LWord;
uint8_t um_UInt8;
uint16_t um_UInt16;
uint32_t um_UInt32;
uint64_t um_UInt64;
int8_t um_Int8;
int16_t um_Int16;
int32_t um_Int32;
int64_t um_Int64;
float um_Float;
double um_DFloat;
};
uAnyData m_uAnyData;
};
So I was looking for alternate solution using template
class CDataType {
public:
virtual void get() = 0;
}
template<class T> MyDataType public CDataType{
private:
private T data_;
public:
MyVariantType(T data) {
data_ = data;
}
virtual T get() {
return data_;
}
}
CDataType* var = new MyDataType<int>(100);
var->get();
var = new MyDataType<string>("hello world");
var->get();
I'm not so clear about what you want, but from one of your comments, it seems like this might be useful. (Or at least you might get some ideas from the
code.)
The TPun class is pointed at some location in memory, either with a pointer or reference to an object, and it allows you to read values from arbitrary offsets from the start of the buffer.
Look at the demonstration at the in main() (at the bottom) to see how it can be used.
#include <iostream>
#include <string>
#include <sstream>
#include <cstring>
#include <cstddef>
class TPun {
const void* pbuffer;
template <typename IT>
const void* offset_ptr(IT byte_offset) const {
return static_cast<const void*>(static_cast<const char*>(pbuffer) + byte_offset);
}
public:
TPun() : pbuffer(nullptr) {}
template <typename T>
TPun(const T* pbuffer) : pbuffer(static_cast<const void*>(pbuffer)) {}
template <typename T>
TPun(T* pbuffer) : pbuffer(static_cast<const void*>(pbuffer)) {}
template <typename T>
TPun(const T& struct_obj) : pbuffer(static_cast<const void*>(&struct_obj)) {}
template <typename T>
TPun& operator = (const T* pbuf) { pbuffer = static_cast<const void*>(pbuf); return *this; }
template <typename T>
TPun& operator = (T* pbuf) { pbuffer = static_cast<const void*>(pbuf); return *this; }
template <typename T>
TPun& operator = (const T& struct_obj) { pbuffer = static_cast<const void*>(&struct_obj); return *this; }
template <typename T>
struct is_cchar_ptr {
template <typename U> static U& dval();
static int test(char const * const &) { return true; }
template <typename U>
static char test(const U&) { return false; }
static const bool value = sizeof(int) == sizeof(test(dval<T>()));
};
// Returns a reference for non-pointer types (only used for const char*)
template <typename T> struct cptr_pass { static const T& get(const void* p) { return *static_cast<const T*>(p); } };
template <typename T> struct cptr_pass<const T*> { static const T* get(const void* p) { return static_cast<const T*>(p); } };
// at only works for POD types or classes consisting of only POD members
template <typename T, typename IT>
T at(IT byte_offset) const {
if(is_cchar_ptr<T>::value) {
// special handling when T is const char*
// returns a pointer to the buffer instead of a pointer created from buffer data
// using char to access the buffer avoids the strict aliasing rule
return cptr_pass<T>::get(offset_ptr(byte_offset));
}
T value;
std::memcpy(&value, offset_ptr(byte_offset), sizeof(T));
return value;
}
// This version of at() works with any type, but sports Undefined Behavior,
// because it violates the strict aliasing rule.
// This can also cause problems if the offset you give, along with the buffer's
// base address result in a pointer which is not aligned properly for the type.
template <typename T, typename IT>
const T& the_at_of_many_dangers(IT byte_offset) {
return *static_cast<const T&>(offset_ptr(byte_offset));
}
std::ostream& manipulate(std::ostream& is) { return is; }
template<typename M, typename ...Ms>
std::ostream& manipulate(std::ostream& is, const M& manipulator, Ms ...manips) {
return manipulate(is << manipulator, manips...);
}
template <typename T, typename IT, typename ...Ms>
std::string string_at(IT byte_offset, Ms ...manips) {
std::stringstream ss;
manipulate(ss, manips...) << at<T>(byte_offset);
return ss.str();
}
};
void no_warning_strcpy(char* d, const char* s) { while(*(d++) = *(s++)) {} }
struct test_struct {
char text[10];
float number;
int some_int;
double a_double;
test_struct() {
no_warning_strcpy(text, "hi there");
number = 52.25;
some_int = 1000000000;
a_double = 1.2e-14;
}
};
int main() {
using std::cout;
using std::string;
// create a test_struct (values are filled in automatically)
test_struct ts;
// copy ts's memory image to a char buffer
char* memory_buffer = new char[sizeof(ts)];
std::memcpy(memory_buffer, &ts, sizeof(ts));
// Create a TPun object, start off pointing at the char buffer
TPun tpun(memory_buffer);
// send some field values to cout
// the "offsetof" macro (defined in <cstddef>) gives the
// byte offset of a field in a struct
cout << tpun.at<const char*>(offsetof(test_struct, text)) << '\n';
cout << tpun.at<float>(offsetof(test_struct, number)) << '\n';
// format a value into a string. Optionally, add extra ostream manipulator arguments.
// This makes a string with the data from some_int, and formats it as a hex string
string hex_string = tpun.string_at<int>(offsetof(test_struct, some_int), std::hex);
cout << hex_string << '\n';
// I screwed this one up -- I used the text field's data to make an int
// Using the manipulator this way, instead of going directly through
// cout, keeps cout's flags intact
cout << tpun.string_at<int>(offsetof(test_struct, text), std::hex) << '\n';
// change a_double in the original struct
ts.a_double = -1;
// tpun is pointing at the buffer copy, so it shows the original value
cout << tpun.at<double>(offsetof(test_struct, a_double)) << '\n';
// tpun's target can be chaged on-the-fly like this, and it can be set to
// view an object's memory directly, without creating a pointer.
tpun = ts;
// outputs -1
cout << tpun.at<double>(offsetof(test_struct, a_double)) << '\n';
}
This demo prints:
hi there
52.25
3b9aca00
74206968
1.2e-14
-1
There are some Classes: Array, NumericArray. Array is a template class, and NumericArray is a class inherited from Array designed to take int, double, etc.
Part of Header of NumericArray:
template <class T = int>
class NumericArray : public Array<T>{
private:
T* m_data;
int size;
public:
NumericArray<T> operator * (T factor)const;
};
here are constructors and some functions of NumericArray:
template <class T>
NumericArray<T>::NumericArray(){
m_data = new T[10];
size = 10;
}
template <class T>
NumericArray<T>::NumericArray(int n){
m_data = new T[n];
size = n;
}
template <class T>
NumericArray<T>::NumericArray(const NumericArray<T>& s_data){
m_data = new T[s_data.size];
// assign elements in the source array
for (int i = 0;i<=(s_data.Size()-1 ); i++){
m_data[i] = s_data.m_data[i];
}
size = s_data.Size();
}
/* Destructor */
template <class T>
NumericArray<T>::~NumericArray(){
delete [] m_data;
}
template <class T>
NumericArray<T> NumericArray<T>::operator * (T factor)const{
NumericArray<T> temp(size);
for (int i = 0; i<size;i++){
temp.m_data[i] = (*this)[i] *factor;
}
return temp;
}
And when I call it in the main(), something weird happens. For example:
NumericArray<int> intArray1(10);
NumericArray<int> intArray2;
for(int i =0; i<10;i++){
intArray1[i] = i;
intArray2[i] = i;
}
The 2 arrays do contain numbers 0-9, but if I call
NumericArray intArray4 = intArray1*2;
intArray4 consists of zero(0)s. It seems that the default constructor is called in the function and passed to Array4. And after the operator, Array1 and Array2 are still numbers 0-9
Below are the related code of Array
template class Array{
private:
T* m_data;
int size;
public:
Array(); // constructor
Array(int n); // constructor
Array(const Array<T>& s_data); //Copy Constructor
virtual ~Array(); // destructor
void SetElement(int i, const T& source);
T& GetElement(int i)const;
int Size() const;
int DefaultSize()const;
void DefaultSize(int n);
// Operator overloading
Array<T>& operator = (const Array<T>& source) ;
T& operator [](int i);
const T& operator [] (int i) const;
};
template <class T>
Array<T>::Array(){
// default constructor
m_data = new T[defaultSize]; // initialize T*
size = defaultSize; // initialize integer
}
template <class T>
Array<T>::Array(int n){
// Constructor with arguments
m_data = new T[n];
size = n;
}
template <class T>
Array<T>::Array(const Array<T>& s_data){
// Copy constructor
m_data = new T[s_data.Size()];
// assign elements in the source array
for (int i = 0;i<=(s_data.Size()-1 ); i++){
m_data[i] = T( s_data.m_data[i]);
}
size = s_data.Size();
defaultSize = s_data.Size();
}
template <class T>
T& Array<T>::operator [](int i) {
if (i > size|| i<0){
OutOfBoundsException a;
throw a;
}
return m_data[i];
}
Not sure if provided enough information. Any hint is greatly appreciated.
The easiest way to access base class data members when the base is a (dependent) class template, is a using declaration like this:
#include <iostream>
using namespace std;
template< class Item >
class Base
{
protected:
Item item_;
Base( Item const& v ): item_( v ) {}
};
template< class Item >
class Derived
: public Base< Item >
{
protected:
using Base<Item>::item_;
public:
auto value() const -> Item { return item_; }
Derived( Item const& v ): Base<Item>( v ) {}
};
auto main() -> int
{
Derived<int> const x( 42 );
cout << x.value() << endl;
}
Alternatively you can qualify the access, e.g. this->item_ or Base<Item>::item_.
That said, it’s usually not a good idea to let derived classes access base class data members directly.
Here is the problem: both NumericArray and Array have
T* m_data;
int size;
The function Array::operator[] is called from Array which uses Array::m_data, however the NumericArray::operator* sets NumericAray::m_data. It probably is working as you would expect, but you are reading from the wrong pointer.
Remove the members from NumericArray and make the members protected instead of private in Array. The second part is optional if the implementation is changed a little bit.
It's possible to define a pointer to a member and using this later on:
struct foo
{
int a;
int b[2];
};
int main()
{
foo bar;
int foo::* aptr=&foo::a;
bar.a=1;
std::cout << bar.*aptr << std::endl;
}
Now I need to have a pointer to a specific element of an array, so normally I'd write
int foo::* bptr=&(foo::b[0]);
However, the compiler just complains about an "invalid use of non-static data member 'foo::b'"
Is it possible to do this at all (or at least without unions)?
Edit: I need a pointer to a specific element of an array, so int foo::* ptr points to the second element of the array (foo::b[1]).
Yet another edit: I need to access the element in the array by bar.*ptr=2, as the pointer gets used somewhere else, so it can't be called with bar.*ptr[1]=2 or *ptr=2.
However, the compiler just complains about an "invalid use of non-static data member 'foo::b'"
This is because foo::a and foo::b have different types. More specifically, foo::b is an array of size 2 of ints. Your pointer declaration has to be compatible i.e:
int (foo::*aptr)[2]=&foo::b;
Is it possible to do this at all (or at least without unions)?
Yes, see below:
struct foo
{
int a;
int b[2];
};
int main()
{
foo bar;
int (foo::*aptr)[2]=&foo::b;
/* this is a plain int pointer */
int *bptr=&((bar.*aptr)[1]);
bar.a=1;
bar.b[0] = 2;
bar.b[1] = 11;
std::cout << (bar.*aptr)[1] << std::endl;
std::cout << *bptr << std::endl;
}
Updated post with OP's requirements.
The problem is that, accessing an item in an array is another level of indirection from accessing a plain int. If that array was a pointer instead you wouldn't expect to be able to access the int through a member pointer.
struct foo
{
int a;
int *b;
};
int main()
{
foo bar;
int foo::* aptr=&(*foo::b); // You can't do this either!
bar.a=1;
std::cout << bar.*aptr << std::endl;
}
What you can do is define member functions that return the int you want:
struct foo
{
int a;
int *b;
int c[2];
int &GetA() { return a; } // changed to return references so you can modify the values
int &Getb() { return *b; }
template <int index>
int &GetC() { return c[index]; }
};
typedef long &(Test::*IntAccessor)();
void SetValue(foo &f, IntAccessor ptr, int newValue)
{
cout << "Value before: " << f.*ptr();
f.*ptr() = newValue;
cout << "Value after: " << f.*ptr();
}
int main()
{
IntAccessor aptr=&foo::GetA;
IntAccessor bptr=&foo::GetB;
IntAccessor cptr=&foo::GetC<1>;
int local;
foo bar;
bar.a=1;
bar.b = &local;
bar.c[1] = 2;
SetValue(bar, aptr, 2);
SetValue(bar, bptr, 3);
SetValue(bar, cptr, 4);
SetValue(bar, &foo::GetC<0>, 5);
}
Then you at least have a consistent interface to allow you to change different values for foo.
2020 update, with actual solution:
The Standard does currently not specify any way to actually work with the member pointers in a way that would allow arithmetics or anything to get the pointer to the "inner" array element
OTOH, the standard library now has all the necessities to patch the appropriate member pointer class yourself, even with the array element access.
First, the member pointers are usually implemented as "just offsets", although quite scary. Let's see an example (on g++9, arch amd64):
struct S { int a; float b[10]; };
float(S::*mptr)[10] = &S::b;
*reinterpret_cast<uintptr_t *>(&mptr) //this is 4
int S::*iptr = &S::a;
*reinterpret_cast<uintptr_t *>(&iptr) //this is 0
iptr = nullptr;
*reinterpret_cast<uintptr_t *>(&iptr) //this seems to be 18446744073709551615 on my box
Instead you can make a bit of a wrapper (it's quite long but I didn't want to remove the convenience operators):
#include <type_traits>
template<class M, typename T>
class member_ptr
{
size_t off_;
public:
member_ptr() : off_(0) {}
member_ptr(size_t offset) : off_(offset) {}
/* member access */
friend const T& operator->*(const M* a, const member_ptr<M, T>& p)
{ return (*a)->*p; }
friend T& operator->*(M* a, const member_ptr<M, T>& p)
{ return (*a)->*p; }
/* operator.* cannot be overloaded, so just take the arrow again */
friend const T& operator->*(const M& a, const member_ptr<M, T>& p)
{ return *reinterpret_cast<const T*>(reinterpret_cast<const char*>(&a) + p.off_); }
friend T& operator->*(M& a, const member_ptr<M, T>& p)
{ return *reinterpret_cast<T*>(reinterpret_cast<char*>(&a) + p.off_); }
/* convert array access to array element access */
member_ptr<M, typename std::remove_extent<T>::type> operator*() const
{ return member_ptr<M, typename std::remove_extent<T>::type>(off_); }
/* the same with offset right away */
member_ptr<M, typename std::remove_extent<T>::type> operator[](size_t offset) const
{ return member_ptr<M, typename std::remove_extent<T>::type>(off_)+offset; }
/* some operators */
member_ptr& operator++()
{ off_ += sizeof(T); return *this; };
member_ptr& operator--()
{ off_ -= sizeof(T); return *this; };
member_ptr operator++(int)
{ member_ptr copy; off_ += sizeof(T); return copy; };
member_ptr operator--(int)
{ member_ptr copy; off_ -= sizeof(T); return copy; };
member_ptr& operator+=(size_t offset)
{ off_ += offset * sizeof(T); return *this; }
member_ptr& operator-=(size_t offset)
{ off_ -= offset * sizeof(T); return *this; }
member_ptr operator+(size_t offset) const
{ auto copy = *this; copy += offset; return copy; }
member_ptr operator-(size_t offset) const
{ auto copy = *this; copy -= offset; return copy; }
size_t offset() const { return off_; }
};
template<class M, typename T>
member_ptr<M, T> make_member_ptr(T M::*a)
{ return member_ptr<M, T>(reinterpret_cast<uintptr_t>(&(((M*)nullptr)->*a)));}
Now we can make the pointer to the array element directly:
auto mp = make_member_ptr(&S::b)[2];
S s;
s->*mp = 123.4;
// s.b[2] is now expectably 123.4
Finally, if you really, really like materialized references, you may get a bit haskell-lensish and make them compose:
// in class member_ptr, note transitivity of types M -> T -> TT:
template<class TT>
member_ptr<M,TT> operator+(const member_ptr<T,TT>&t)
{ return member_ptr<M,TT>(off_ + t.offset()); }
// test:
struct A { int a; };
struct B { A arr[10]; };
B x;
auto p = make_member_ptr(&B::arr)[5] + make_member_ptr(&A::a)
x->*p = 432.1;
// x.arr[5].a is now expectably 432.1
typedef int (foo::*b_member_ptr)[2];
b_member_ptr c= &foo::b;
all works.
small trick for member and function pointers usage.
try to write
char c = &foo::b; // or any other function or member pointer
and in compiller error you will see expected type, for your case int (foo::*)[2].
EDIT
I'm not sure that what you want is legal without this pointer. For add 1 offset to your pointer you should get pointer on array from your pointer on member array. But you can dereference member pointer without this.
You can't do that out of the language itself. But you can with boost. Bind a functor to some element of that array and assign it to a boost::function:
#include <boost/lambda/lambda.hpp>
#include <boost/lambda/bind.hpp>
#include <boost/function.hpp>
#include <iostream>
struct test {
int array[3];
};
int main() {
namespace lmb = boost::lambda;
// create functor that returns test::array[1]
boost::function<int&(test&)> f;
f = lmb::bind(&test::array, lmb::_1)[1];
test t = {{ 11, 22, 33 }};
std::cout << f(t) << std::endl; // 22
f(t) = 44;
std::cout << t.array[1] << std::endl; // 44
}
I'm not sure if this will work for you or not, but I wanted to do a similar thing and got around it by approaching the problem from another direction. In my class I had several objects that I wanted to be accessible via a named identifier or iterated over in a loop. Instead of creating member pointers to the objects somewhere in the array, I simply declared all of the objects individually and created a static array of member pointers to the objects.
Like so:
struct obj
{
int somestuff;
double someotherstuff;
};
class foo
{
public:
obj apples;
obj bananas;
obj oranges;
static obj foo::* fruit[3];
void bar();
};
obj foo::* foo::fruit[3] = { &foo::apples, &foo::bananas, &foo::oranges };
void foo::bar()
{
apples.somestuff = 0;
(this->*(fruit[0])).somestuff = 5;
if( apples.somestuff != 5 )
{
// fail!
}
else
{
// success!
}
}
int main()
{
foo blee;
blee.bar();
return 0;
}
It seems to work for me. I hope that helps.