P
__PPS__
suppose I have class that has templated operator[].
I want to have a few overloads/template specializations for different
types. Basicly, the biggest problem I have is to be able to have this:
const char a[] = "this is passed as an array";
const char *p = "this is passed by the address";
x_class x;
x[a] = .. whatever ..;
x[p] = ..;
then x[a] and x[p] call different overloads. For example inside of the
operator[](...) I want to get the length/size of the passed string. In
case if an array is used it's size is known at compile time and with
pointer I need to calculate the length.
I have a solution, but I'm looking for a better example on how to do
this (Note - it should be templated operator[] and not overloads of
functions or operator())
My solution: (a working example of what I mean)
/////////////
#include <iostream>
#include <cstring>
using namespace std;
class x_class{
template<class T>struct size_of{
static inline size_t value(const T &t){
cout << "dynamic version used" << endl;
return strlen(t);
}
};
template<class T, size_t N>struct size_of<T[N]>{
template<class C>static inline size_t value(const C &){
cout << "static version used" << endl;
return sizeof(C) - 1;
}
};
public:
template<class T>void operator[](const T &t)const{
const char *str = &t[0];
size_t len = size_of<T>::value(t);
(cout << "passed string => \"").write(str,len) << "\"\n\n";
}
};
int main(int,char*[]){
const char a[] = "this is passed as an array";
const char *p = "this is passed by the address";
x_class x;
x[a]; //length known at compile time
x[p]; //length is calculated at runtime
x["some other string"]; //known at compile time
}
////////
PS I'm also interested to have operator[](int) and operator[](int []
and int *)
and there must be no confusion if I use x[0] (eg const char* or int??)
thanks
I want to have a few overloads/template specializations for different
types. Basicly, the biggest problem I have is to be able to have this:
const char a[] = "this is passed as an array";
const char *p = "this is passed by the address";
x_class x;
x[a] = .. whatever ..;
x[p] = ..;
then x[a] and x[p] call different overloads. For example inside of the
operator[](...) I want to get the length/size of the passed string. In
case if an array is used it's size is known at compile time and with
pointer I need to calculate the length.
I have a solution, but I'm looking for a better example on how to do
this (Note - it should be templated operator[] and not overloads of
functions or operator())
My solution: (a working example of what I mean)
/////////////
#include <iostream>
#include <cstring>
using namespace std;
class x_class{
template<class T>struct size_of{
static inline size_t value(const T &t){
cout << "dynamic version used" << endl;
return strlen(t);
}
};
template<class T, size_t N>struct size_of<T[N]>{
template<class C>static inline size_t value(const C &){
cout << "static version used" << endl;
return sizeof(C) - 1;
}
};
public:
template<class T>void operator[](const T &t)const{
const char *str = &t[0];
size_t len = size_of<T>::value(t);
(cout << "passed string => \"").write(str,len) << "\"\n\n";
}
};
int main(int,char*[]){
const char a[] = "this is passed as an array";
const char *p = "this is passed by the address";
x_class x;
x[a]; //length known at compile time
x[p]; //length is calculated at runtime
x["some other string"]; //known at compile time
}
////////
PS I'm also interested to have operator[](int) and operator[](int []
and int *)
and there must be no confusion if I use x[0] (eg const char* or int??)
thanks