Topic: exactly equal type testing and new casts
Author: Alex Rosenberg <alexr@spies.com>
Date: Tue, 12 Jun 2001 21:23:06 GMT Raw View
In article <BU_U6.3469$pb1.130349@www.newsranger.com>,
Michiel Salters<Michiel.Salters@cmg.nl> wrote:
>In article <alexr-15714F.01041108062001@news.apple.com>, Alex Rosenberg says...
>
>>In some cases, type_info's operator== is too heavyweight. I'd
>>occasionally like to only test for an exact class match.
>>
>>Lack of this forces classes to add a member to uniquely identify them.
>
>template <typename T1, typename T2>
>class is_equal_t {
>enum {is_equal=false};
>};
>
>template <typename T1>
>class is_equal_t<T1, T1> {
>enum {is_equal=true};
>};
>
>// Helper, for runtime only.
>template <typename T1, typename T2>
>bool is_equal_type() { return is_equal_t<T1,T2>::is_equal; }
>
>int main()
>{
>is_equal_type<int, int>(); // true
>is_equal_type<int, double>(); // false
>}
A beautfully crafted solution, but I was aactually seeking a runtime
test, not a compile-time one.
+------------------------------------------------------------+
| Alexander M. Rosenberg <mailto:alexr@_spies.com> |
| Nobody cares what I say. Remove the underscore to mail me. |
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: Michiel Salters<Michiel.Salters@cmg.nl>
Date: Mon, 11 Jun 2001 08:43:52 GMT Raw View
In article <alexr-15714F.01041108062001@news.apple.com>, Alex Rosenberg says...
>In some cases, type_info's operator== is too heavyweight. I'd
>occasionally like to only test for an exact class match.
>
>Lack of this forces classes to add a member to uniquely identify them.
template <typename T1, typename T2>
class is_equal_t {
enum {is_equal=false};
};
template <typename T1>
class is_equal_t<T1, T1> {
enum {is_equal=true};
};
// Helper, for runtime only.
template <typename T1, typename T2>
bool is_equal_type() { return is_equal_t<T1,T2>::is_equal; }
int main()
{
is_equal_type<int, int>(); // true
is_equal_type<int, double>(); // false
}
--
Michiel Salters
Consultant Technical Software Engineering
CMG Trade, Transport & Industry
Michiel.Salters@cmg.nl
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: Alex Rosenberg <alexr@spies.com>
Date: Fri, 8 Jun 2001 19:22:52 GMT Raw View
In some cases, type_info's operator== is too heavyweight. I'd
occasionally like to only test for an exact class match.
Lack of this forces classes to add a member to uniquely identify them.
"Modern C++ Design" shows a method where a static member is
automatically uniqified to work around this. In most implementations,
this means at least two memory references per class being compared. It
would be desirable to reduce this to just one reference (the RTTI ptr
itself).
One of the most common old-style casts on classic Mac OS is conversion
of string pointers from signed to unsigned and vice-versa. static_cast<>
would be more desirable. Similarly, volatile_cast<> might be added.
+------------------------------------------------------------+
| Alexander M. Rosenberg <mailto:alexr@_spies.com> |
| Nobody cares what I say. Remove the underscore to mail me. |
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]