Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Limiting function input only to __int64 but not int...

Reply
Thread Tools

Limiting function input only to __int64 but not int...

 
 
A
Guest
Posts: n/a
 
      10-24-2011

"Goran" <> wrote in message
news:c59f1d33-57bf-47f9-b23a-...
>Out of curiosity... Why? int "is an" __int64 any way you look at this.
>Why would it matter?


because function requires explicitly __int64 because ID's are 64-bit,
providing 32-bit int would ruin ID's that go over the 32-bit boundary. This
may happen when I do text-to-int conversions using StrToInt and forget to
put StrToInt64 instead.

In Borlangearcadero C++ Builder 2010 which I use, these functions output int
and __int64 so compiler SHOULD be able to check these but unfortunately it
doesn't - even if I enable W8102 Implicit conversion of '%s' to '%s' (C++)
but it doesn't do what it is supposed to. I tried casting to wrong type but
the warning is still not triggered.


 
Reply With Quote
 
 
 
 
Richard Damon
Guest
Posts: n/a
 
      10-24-2011
On 10/24/11 1:17 PM, Robert Wessel wrote:
> On Mon, 24 Oct 2011 17:11:48 +0200, "A"<> wrote:
>
>>
>> "Goran"<> wrote in message
>> news:c59f1d33-57bf-47f9-b23a-...
>>> Out of curiosity... Why? int "is an" __int64 any way you look at this.
>>> Why would it matter?

>>
>> because function requires explicitly __int64 because ID's are 64-bit,
>> providing 32-bit int would ruin ID's that go over the 32-bit boundary. This
>> may happen when I do text-to-int conversions using StrToInt and forget to
>> put StrToInt64 instead.
>>
>> In Borlangearcadero C++ Builder 2010 which I use, these functions output int
>> and __int64 so compiler SHOULD be able to check these but unfortunately it
>> doesn't - even if I enable W8102 Implicit conversion of '%s' to '%s' (C++)
>> but it doesn't do what it is supposed to. I tried casting to wrong type but
>> the warning is still not triggered.

>
>
> Why not use the usual trick of imbedding the handle in a structure?
>
> struct MyHandle(__int64 h;};
>
> And use a MyHandle as the parameter or return value?
>
> Alternatively, explicitly declare forms for those functions using int,
> short, etc., and then never define them - that way the link will fail.
> Better yet, if your compiler supports the new standard, use the
> "=delete" form:
>
> void f(__int64); //supply actual definition
> void f(int)=delete; //can't be called
>
> Of course that's ugly because of the number of definitions you need to
> supply to cover all of the usual conversions.


Actually, once you define one disallowed form, the rest become
ambiguous. For example, calling with a "short" neither short->int or
short->__int64 has a preference, so I believe it should give an error. I
don't think the =delete removes that version from overload resolution,
but I haven't looked at it in detail yet.

 
Reply With Quote
 
 
 
 
A
Guest
Posts: n/a
 
      10-24-2011
"Robert Wessel" <> wrote in message
news:...
> void f(__int64); //supply actual definition
> void f(int)=delete; //can't be called


I actually like this solution but the compiler doesn't support it. I guess
I'll be looking for alternate solutions.


 
Reply With Quote
 
Richard Damon
Guest
Posts: n/a
 
      10-25-2011
On 10/24/11 6:12 PM, A wrote:
> "Robert Wessel"<> wrote in message
> news:...
>> void f(__int64); //supply actual definition
>> void f(int)=delete; //can't be called

>
> I actually like this solution but the compiler doesn't support it. I guess
> I'll be looking for alternate solutions.
>
>


if it doesn't support the =delete syntax, declaring (but not defining)
void f(unsigned char); (choose an integer type that you are actually
unlikely to mistakenly use) will make a call of f(1) ambiguous and thus
an error. You want to make the undefined type an unlikely mistake as if
you use it by mistake, you are just going to get a link error, and you
will have to search to find where it is actually called.
 
Reply With Quote
 
Arne Mertz
Guest
Posts: n/a
 
      10-26-2011
On Oct 25, 7:27*am, Christian Gollwitzer <aurio...@gmx.de> wrote:
> Maybe it supports #pragma error?
> Then you can write


#pragma error is not very portable, e.g. MSVC does not
sopport it, instead, it issues a warning about the
unsupported pragma and happily continues compiling,
i.e. f(int) may be called.

overloading f(int) and f(__int64) imo is not a good idea,
because once you port the whole thing to a 64 bit machine
int and __int64 become the same and you get either a
redefinition error (if you actually defined f(int)), or
the whole "error detection via link error or ambiguity"
magically dissapears if you only declared f(int), as it
is just another declaration of f(__int64).
In your case the latter behavior may be ok, if the
problem is only about disambiguating 32 bit int and __in64
in 32 bit systems. But maybe StrToInt outputs __int32
even on 64 bit systems or something similar happens so you
want to keep up the 32 bit integer detection even on 64 bit
systems.

To make things short(er), I would use overloading, but not
with any specific integer type but with a template:
given f(__int64) as the function to be called only for
64bit types, you have several variants

Variant a)
template <typename T>
void f(T) = delete; //overload resolution calls this for
any T that is not the same as __int64

Variant b), C++0x without deleted functions
template <typename T>
void f(T)
{ static_assert(false, "call f only with __int64!"); }

Variant c), C++0x, less restrictive
#include <type_traits>
template <typename T>
f(T t) -> decltype(f(__int64))
{
// assert T is "sufficiently close to" __int64
static_assert(std::is_integral<T>::value
&& std::is_convertible<T, __int64>::value
&& sizeof(T) >= sizeof(__int64));

return f(__int64(t));
}

If your compiler lacks support for static_assert, you
could use either std::enable_if or the STATIC_ASSERT
macro from the loki library. If your standard library
has no <type_traits>, boost provides similar utilities.

HTH Arne





 
Reply With Quote
 
 
 
Reply

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 On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
6500: User-Based Rate Limiting AND Total Rate Limiting Patrick Cervicek Cisco 0 08-07-2007 03:07 PM
function returning __int32 in place of __int64 hurry C Programming 8 12-29-2005 09:59 AM
__int64 vs. long long Tim Clacy C++ 8 11-10-2003 03:29 PM
unsigned __int64 overflow problem Vivi C++ 2 08-25-2003 06:09 AM
Re: Different results if compared as double or __int64? John Tsiombikas (Nuclear / the Lab) C++ 1 06-30-2003 02:31 PM



Advertisments