explicit type conversion

  • Follow


I have a fixed-point math class, and to avoid unintentional conversions 
to built-in types, while allowing intentional ones, I'd like to do this:

   struct Fixed {

	/* ... */

	explicit operator int() const { /* blah */ };
   };

And then:

   Fixed a;
   int b;

   /* ... */

   b = a;	// compiler error
   b = int(a);	// okey dokey

Sound like something that would be nice for C++ to do? Is there another 
way to achieve this effect? (That is, without building your own new 
overloaded function.)

-thant


      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply Thant 5/5/2004 8:39:57 PM

On 5 May 2004 16:39:57 -0400, Thant Tessman <thant@acm.org> wrote:

 >
 >I have a fixed-point math class, and to avoid unintentional conversions
 >to built-in types, while allowing intentional ones, I'd like to do this:
 >
 >   struct Fixed {
 >
 >       /* ... */
 >
 >       explicit operator int() const { /* blah */ };
 >   };
 >
 >And then:
 >
 >   Fixed a;
 >   int b;
 >
 >   /* ... */
 >
 >   b = a;      // compiler error
 >   b = int(a); // okey dokey
 >
 >Sound like something that would be nice for C++ to do? Is there another
 >way to achieve this effect? (That is, without building your own new
 >overloaded function.)

I think a simple a simple "asInt" or "toInt" member function would be
preferable in non-generic code.  However, I guess it could be useful
in generic code where it would be difficult to generate the name of
such a function from a type.  Is that what you had in mind?

Steve

www.semantics.org


      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply Stephen 5/6/2004 9:23:25 AM


Thant Tessman <thant@acm.org> writes:

 > I have a fixed-point math class, and to avoid unintentional conversions
 > to built-in types, while allowing intentional ones, I'd like to do this:
 >
 >    struct Fixed {
 >
 >       /* ... */
 >
 >       explicit operator int() const { /* blah */ };

Somewhere in that great machine in the sky which restarts old usenet
     threads, there must be a cron job which asks for this feature.

 >    };
 >
 > And then:
 >
 >    Fixed a;
 >    int b;
 >
 >    /* ... */
 >
 >    b = a;     // compiler error
 >    b = int(a);        // okey dokey
 >
 > Sound like something that would be nice for C++ to do? Is there another
 > way to achieve this effect? (That is, without building your own new
 > overloaded function.)

That would be like naming a function 'add' instead of '+' :-)

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply llewelly 5/6/2004 9:27:52 AM

Thant Tessman <thant@acm.org> wrote in message news:<c7b5qv$gc5$1@terabinaries.xmission.com>...
> I have a fixed-point math class, and to avoid unintentional conversions 
> to built-in types, while allowing intentional ones, I'd like to do this:
> 
>    struct Fixed {
> 
> 	/* ... */
> 
> 	explicit operator int() const { /* blah */ };
>    };
> 
> And then:
> 
>    Fixed a;
>    int b;
> 
>    /* ... */
> 
>    b = a;	// compiler error
>    b = int(a);	// okey dokey
> 
> Sound like something that would be nice for C++ to do? Is there another 
> way to achieve this effect? (That is, without building your own new 
> overloaded function.)
> 
> -thant

What you are trying to do is not currently supported in C++.  However
it was the subject of a recent discussion either in this NG or in
c.l.c++ or c.std.c++ .. I can't remember the precise title, but hey,
that's what google is for,right? <grin>

Dave Moore

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply dtmoore 5/6/2004 2:10:05 PM

Stephen C. Dewhurst wrote:
 > On 5 May 2004 16:39:57 -0400, Thant Tessman <thant@acm.org> wrote:

[...]

 >  >
 >  >       explicit operator int() const { /* blah */ };
 >  >

[...]

 > I think a simple a simple "asInt" or "toInt" member function would be
 > preferable in non-generic code.

A 'toInt' function is indeed the workaround I chose.


 >  However, I guess it could be useful
 > in generic code where it would be difficult to generate the name of
 > such a function from a type.  Is that what you had in mind?

I have a ton of math code originally parameterized to allow me to switch
painlessly between float and double. I recently had the need to
parameterize the code on a new custom fixed-point type, and for all the
effort put into allowing C++ programmers to build their own data types
that behave like built-in data types, I was surprised to discover this
effort was incomplete in this (admittedly somewhat nitpicky) way.

Just curious about other people's thoughts on the issue...

-thant


      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply Thant 5/7/2004 11:57:36 AM

4 Replies
140 Views

(page loaded in 0.085 seconds)

Similiar Articles:













7/26/2012 9:27:43 AM


Reply: