Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old August 17th, 2005, 06:05 AM
martin
Guest
 
Posts: n/a
Default reference/pointer to arrays type-cast operators

hi guys,

have you ever tried to create a type-cast operator to a
'reference/pointer to an array'? i hadn't, not until yesterday when i
discovered that it is not exactly the trivial task one'd assume it to
be - none of my apparently naive attempts were accepted for a valid
type-cast operator by any of the compilers i threw them at (i.e. gcc
2.95.3, gcc 3.3.3, vc 2003).
so assume we have:

template < unsigned T >
class A
{
float m[T];
};

which we want to be able to cast to the data type of its sole data
member m. a supposedly correct way would be a member operator:

operator const float (& () const)[T];

which unfortunately ended up in a bunch of syntax errors on all
compilers tested; then the 'out of ideas' attempt:

oprator const float (&)[T] () const;

just got me accused (and rightfully so) of trying to declare an
operator as an array of functions.

so please tell me i was being stupid and i missed something obvious
here.

regards
martin 'blu' krastev

  #2  
Old August 17th, 2005, 06:15 AM
Victor Bazarov
Guest
 
Posts: n/a
Default Re: reference/pointer to arrays type-cast operators

martin wrote:[color=blue]
> have you ever tried to create a type-cast operator to a
> 'reference/pointer to an array'? i hadn't, not until yesterday when i
> discovered that it is not exactly the trivial task one'd assume it to
> be - none of my apparently naive attempts were accepted for a valid
> type-cast operator by any of the compilers i threw them at (i.e. gcc
> 2.95.3, gcc 3.3.3, vc 2003).
> so assume we have:
>
> template < unsigned T >
> class A
> {
> float m[T];
> };
>
> which we want to be able to cast to the data type of its sole data
> member m. a supposedly correct way would be a member operator:
>
> operator const float (& () const)[T];
>
> which unfortunately ended up in a bunch of syntax errors on all
> compilers tested; then the 'out of ideas' attempt:
>
> oprator const float (&)[T] () const;
>
> just got me accused (and rightfully so) of trying to declare an
> operator as an array of functions.
>
> so please tell me i was being stupid and i missed something obvious
> here.[/color]

Use a typedef:
--------------------------------------------------------

template<unsigned T> class A
{
float m[T];
public:
typedef const float (&rcfa)[T]; // this should help
operator rcfa() const { return m; }
};

void foo(float const (&rfa)[5])
{
}

int main() {
A<5> a;
foo(a);
A<10> aa;
foo(aa); // error
}
--------------------------------------------------------

V


 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles