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: exact difference between C style and static_cast, const_cast ...... cast, > a reinterpret_cast (5.2.10), or > a reinterpret_cast followed by a const_cast, > can be performed using the cast notation of explicit type conversion. Why no std::back_insert_iterator::value_type? - comp.lang.c++ ...5.2.3/2 (Explicit type conversion (functional notation): "If the expression list is a single expression, the type conversion expression is equivalent (in de- finedness ... boolean to std_logic - comp.lang.vhdlVHDL is a strongly typed language; thus, all the type conversions should be explicit. It should save one's life, some day. In addition, I've discovered a Boolean type ... design pattern for a file converter... - comp.lang.java.programmer ...Then, I refactor to make the patterns explicit if this seems to be of any ... The Portable Document Format (PDF) conversion file type has many ... How to Use Auto Fill ... Oracle SQL bug in 9.2.0.8 - comp.databases.oracle.server ...What I did with my SQL example was to force Oracle to use my explicit conversion rather ... Documented or not, a data type is a data type and if you do something ... Need help with map projection conversion in ENVI - comp.lang.idl ...... of the scientific data set, but there are no explicit ... temp.img' ENVI_WRITE_ENVI_FILE, data, DATA_TYPE=data ... Need help with map projection conversion in ENVI - comp ... counting the digits in a number, exponential format problem - comp ...... language which means > it doesn't require (nor support for that matter) explicit type ... similar to the Linux > function strtod(3) to do its string to float conversions ... How to convert characters to number - comp.lang.fortranOne should generally make decimal points explicit, except ... Fortran program: ACCEPT 10,A,B TYPE 10,A ... F66, section 7.2.3.6 "With all numeric input conversions ... new Graph function + combinatorica: various problems - comp.soft ...I posted on graph conversion a couple of weeks ago. ... anybody tell me a convenient way to convert an explicit ... (* Convert Combinatorica type Graph object to the list ... VHDL-2002 vs VHDL-93 vs VHDL-87? - comp.lang.vhdlA type conversion whose expression is a globally static expression ... the expression is of type STD_ULOGIC, or an array type whose element type is STD_ULOGIC, since explicit ... Type conversion - Wikipedia, the free encyclopediaIn the C family of languages, the word cast typically refers to an explicit type conversion (as opposed to an implicit conversion), regardless of whether this is a re ... Explicit Type Conversion Operator: () - Microsoft Corporation ...C++ allows explicit type conversion using syntax similar to the function-call syntax. 7/26/2012 9:27:43 AM
|