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-23-2011
Is there a way (except overloading) to limit function input to accept
__int64 but to get a compiler (preferable) or runtime (less preferable)
error if it is called with int?

for example:

void myfunction(__int64 param) // Accepts only __int64 as input and no
smaller
{
}

myfunction((__int64)1); // Call OK
myfunction((int)1); // Call fails or better - compiler reports error


 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      10-23-2011
On 10/24/11 11:28 AM, A wrote:
> Is there a way (except overloading) to limit function input to accept
> __int64 but to get a compiler (preferable) or runtime (less preferable)
> error if it is called with int?
>
> for example:
>
> void myfunction(__int64 param) // Accepts only __int64 as input and no
> smaller
> {
> }
>
> myfunction((__int64)1); // Call OK
> myfunction((int)1); // Call fails or better - compiler reports error


You can use a function template to force a link error:

#include <stdint.h>

template <typename T> void f( T n );

template <> void f( int64_t n ) // Specialise for acceptable types
{
}

Or give the function template a body to force a compiler warning:

template <typename T> void f( T n )
{
T tmp = n/0;
}

--
Ian Collins
 
Reply With Quote
 
 
 
 
Marc
Guest
Posts: n/a
 
      10-23-2011
"A" wrote:

> Is there a way (except overloading) to limit function input to accept
> __int64 but to get a compiler (preferable) or runtime (less preferable)
> error if it is called with int?


Assuming that __int64 is not the same type as int.

with overloading (what's wrong with overloading?):
void f(long){}
void f(int)=delete;

with sfinae:
template<class T,class=typename std::enable_if<std::is_same<T,long>::value>::type> void f(T){}

 
Reply With Quote
 
A
Guest
Posts: n/a
 
      10-23-2011
Thank you for your answers so far. I was hoping though for something without
templates. I did found though BOOST_STRONG_TYPEDEF which pretty much does
this.


 
Reply With Quote
 
Goran
Guest
Posts: n/a
 
      10-24-2011
On Oct 24, 12:28*am, "A" <a...@a.a> wrote:
> Is there a way (except overloading) to limit function input to accept
> __int64 but to get a compiler (preferable) or runtime (less preferable)
> error if it is called with int?


Out of curiosity... Why? int "is an" __int64 any way you look at this.
Why would it matter?

Goran.
 
Reply With Quote
 
Marcel Müller
Guest
Posts: n/a
 
      10-24-2011
On 24.10.2011 00:28, A wrote:
> Is there a way (except overloading) to limit function input to accept
> __int64 but to get a compiler (preferable) or runtime (less preferable)
> error if it is called with int?


> for example:
>
> void myfunction(__int64 param) // Accepts only __int64 as input and no
> smaller

void myfunction(const __int64& param);


Marcel
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      10-24-2011
On 10/24/11 08:18 PM, Marcel Müller wrote:
> On 24.10.2011 00:28, A wrote:
>> Is there a way (except overloading) to limit function input to accept
>> __int64 but to get a compiler (preferable) or runtime (less preferable)
>> error if it is called with int?

>
>> for example:
>>
>> void myfunction(__int64 param) // Accepts only __int64 as input and no
>> smaller

> void myfunction(const __int64& param);


Won't the normal promotion rules still apply?

myfunction( 2 );

Compiles happily as does

int n = 0;
myfunction( n );

--
Ian Collins
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      10-24-2011
On 10/24/11 08:53 PM, Ian Collins wrote:
> On 10/24/11 08:18 PM, Marcel Müller wrote:
>> On 24.10.2011 00:28, A wrote:
>>> Is there a way (except overloading) to limit function input to accept
>>> __int64 but to get a compiler (preferable) or runtime (less preferable)
>>> error if it is called with int?

>>
>>> for example:
>>>
>>> void myfunction(__int64 param) // Accepts only __int64 as input and no
>>> smaller

>> void myfunction(const __int64& param);

>
> Won't the normal promotion rules still apply?
>
> myfunction( 2 );
>
> Compiles happily as does
>
> int n = 0;
> myfunction( n );


Hit send too soon, I was going to add that

void myfunction( int64_t& param );

was probably what you intended to type.

--
Ian Collins
 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      10-24-2011
Ian Collins <ian-> wrote:
> Or give the function template a body to force a compiler warning:
>
> template <typename T> void f( T n )
> {
> T tmp = n/0;
> }


I don't think that's a very good idea.
 
Reply With Quote
 
none
Guest
Posts: n/a
 
      10-24-2011
In article <4ea51cef$0$2800$>,
Juha Nieminen <> wrote:
>Ian Collins <ian-> wrote:
>> Or give the function template a body to force a compiler warning:
>>
>> template <typename T> void f( T n )
>> {
>> T tmp = n/0;
>> }

>
> I don't think that's a very good idea.


A compile time assertion would be a bit safer.

 
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