C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

templated operators for arrays and pointers

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
__PPS__
Guest





PostPosted: Fri Jul 29, 2005 12:28 pm    Post subject: templated operators for arrays and pointers Reply with quote



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 template<class C>static inline size_t value(const C &){
cout << "static version used" << endl;
return sizeof(C) - 1;
}
};

public:
template const char *str = &t[0];
size_t len = size_of<T>::value(t);
(cout << "passed string => "").write(str,len) << ""nn";
}
};


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

Back to top
Victor Bazarov
Guest





PostPosted: Fri Jul 29, 2005 1:41 pm    Post subject: Re: templated operators for arrays and pointers Reply with quote



__PPS__ wrote:
Quote:
[...]
and there must be no confusion if I use x[0] (eg const char* or int??)

There should be no confusion to a C++ programmer: the type of 0 is 'int'.

I'll think a bit more about your other questions.

V

Back to top
__PPS__
Guest





PostPosted: Fri Jul 29, 2005 4:32 pm    Post subject: Re: templated operators for arrays and pointers Reply with quote



for me it's clear if I call operator[](0), operaor[](size_t) should be
used, but 0 is also a pointer, so there's ambiguos function call if
both operator[](void* or char*) and op[](size_t) are defined.
there's no problem in this case if I do operator[](size_t)0), or
if there's nontemplated version of opearator[](size_t) overload, and
op[] is templated for pointer type (that was my workaround when I had
op[](size_t) and op[](const char*) at the same time and somewhere in
code I had x[0], which gave error (I think both in gcc and vc71); In
that case I rewrote overload for operator[](const char*) as
template<class T>op[](const T*) and it worked)

check by adding to x_class:
void operator()(size_t i){ cout << "operator()(size_t)n"; }
void operator()(const char *i){ cout << "operator()(const char *)n";
}
then x[0] is compilation error;
if you change op()(const char*) into:
template "operator()(const char *)n"; }
tjen x[0] calls size_t version without errors

Back to top
Victor Bazarov
Guest





PostPosted: Fri Jul 29, 2005 4:53 pm    Post subject: Re: templated operators for arrays and pointers Reply with quote

__PPS__ wrote:
Quote:
for me it's clear if I call operator[](0), operaor[](size_t) should be
used,

Why is it clear? '0' is an "int", not a "size_t". 'int' -> 'size_t'
is a standard integral conversion, which has the same rank as the
conversion from '0' to a pointer (13.3.3.1.1/3).

Quote:
but 0 is also a pointer,

No, it is NOT. It's a literal of type 'int'.

Quote:
so there's ambiguos function call if
both operator[](void* or char*) and op[](size_t) are defined.

Yes. Integral conversions and pointer conversions have the same rank.

Quote:
there's no problem in this case if I do operator[](size_t)0), or
if there's nontemplated version of opearator[](size_t) overload, and
op[] is templated for pointer type (that was my workaround when I had
op[](size_t) and op[](const char*) at the same time and somewhere in
code I had x[0], which gave error (I think both in gcc and vc71); In
that case I rewrote overload for operator[](const char*) as
template<class T>op[](const T*) and it worked)

check by adding to x_class:
void operator()(size_t i){ cout << "operator()(size_t)n"; }
void operator()(const char *i){ cout << "operator()(const char *)n";
}
then x[0] is compilation error;
if you change op()(const char*) into:
template "operator()(const char *)n"; }
tjen x[0] calls size_t version without errors


Don't use 'size_t'. Use 'int'.

V

Back to top
__PPS__
Guest





PostPosted: Tue Aug 02, 2005 8:36 pm    Post subject: Re: templated operators for arrays and pointers Reply with quote

the best solution I found uses enable_if, mpl, and type_traits from
boost:

void operator[](const int i)const{
cout << "[int]n";
}
void operator[](const std::string &s)const{
cout << "[std::string]n";
}
template cout << "[array of " << N << " elements]n";
}
template typename enable_if<
mpl::and_ is_same<typename remove_pointer
Quote:
::type operator[](const T t)const{
cout << "[pointer]n";

}


now it correctly calls array version, pointer, integer or any other
supplied overload (as with string&)


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.