6 Replies - 391 Views - Last Post: 12 November 2009 - 10:37 AM Rate Topic: -----

#1 diego_pmc  Icon User is offline

  • D.I.C Addict

Reputation: 81
  • View blog
  • Posts: 565
  • Joined: 13-May 09

Limits on function parameters

Posted 12 November 2009 - 04:03 AM

I have tried to put some limits on function parameters by using #if and #error but it does not work properly:

#include <iostream>
#include <conio.h>
using namespace std;

void Works(int n) {
    #if n % 2 == 0
    #error 'n' is even
    #endif
    cout << n;
}

int main()
{
    Works(3);
    getch();
}


I get an error on compilation telling me that "'n' is even" even if it is not. This also happens if I add an n = 3; inside the function:

void Works(int n) {
	n = 3;
	#if n % 2 == 0
	#error 'n' is even
	#endif
	cout << n;
}


Is This A Good Question/Topic? 0
  • +

Replies To: Limits on function parameters

#2 EdwinNameless  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 119
  • View blog
  • Posts: 710
  • Joined: 15-October 09

Re: Limits on function parameters

Posted 12 November 2009 - 04:09 AM

Probably where my lack of C++ knowledge will show, but the way I understand this, #if ... etc. are pre-processors instructions, so they occur before actual compiling. In which case "n" cannot have a value yet, since it gets one only at runtime.

What you want is probably an assertion, or even throw an exception.
Was This Post Helpful? 0
  • +
  • -

#3 hackterr  Icon User is offline

  • D.I.C Regular

Reputation: 21
  • View blog
  • Posts: 293
  • Joined: 13-August 09

Re: Limits on function parameters

Posted 12 November 2009 - 04:12 AM

n will be taken as 0 by the preprocessor cos it doesnt have a value available at compile time
what you are thinking is that n will be 3
but at compiling(not running)
works(3)
wont be called
it will be done at runtime

This post has been edited by hackterr: 12 November 2009 - 04:13 AM

Was This Post Helpful? 0
  • +
  • -

#4 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2209
  • View blog
  • Posts: 9,183
  • Joined: 18-February 07

Re: Limits on function parameters

Posted 12 November 2009 - 08:19 AM

As stated earlier the #if/#error/#endif are pre-processor instructuons and are evaluated at compile time... however function arguments are passed at RUNtime. So this bit of code is pretty much useless :)

Generally a simple if-statement can be used to check the bounds of a variable.

void foo(int n) {
	if (n % 2 ==0) {
	  throw "value is even!";
	}
	cout << n;
}


You can also use assertion:
void foo(int n) {
	assert(n % 2 !=0);
	cout << n;
}


Or if you really expect the value to only be inserted by the programmer (i.e. some constant) then you can pass the value as a template parameter.
#include <iostream>

template <int N>
void foo() {
	foo<N % 2>();
}

template <>
void foo<1>() {
	std::cout << "is odd" << std::endl;
}

template <>
void foo<0>() {
	std::cout << "is even" << std::endl;
}

int main() {
 foo<2>();
 foo<9>();
 return 0;
}
Note that it IS possible to get a template metaprogram to throw a compile time error -- however I just don't know enough about it to do it at the moment. ---- its never come up before.
Was This Post Helpful? 0
  • +
  • -

#5 EdwinNameless  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 119
  • View blog
  • Posts: 710
  • Joined: 15-October 09

Re: Limits on function parameters

Posted 12 November 2009 - 09:09 AM

I'm glad I wasn't totally off the mark in my response, then! ;)

On another note, interesting use of templates which I had never seen. Seems pretty drastic, though: is it something commonly used? As far as I'm concerned, I'd tend to use the assertion way, but I'm wondering if it's my heavy Java background that's doing the talk again...
Was This Post Helpful? 0
  • +
  • -

#6 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 844
  • View blog
  • Posts: 2,334
  • Joined: 20-August 07

Re: Limits on function parameters

Posted 12 November 2009 - 10:27 AM

Boost's static_assert (compile time assert) works by giving the compiler something which can't ever possibly compile as the 'false' criteria within a template, for example, a bool template struct declaration which is only specialised for <true> - there's no specialisation for <false>, so the compiler will always fall over complaining that the struct doesn't exist.
template<bool> struct static_assertion;
template<> struct static_assertion<true> {}; 


given something which is always true, the compiler will let it pass
int main()
{
    static_assertion<sizeof(char) <= sizeof(int)>();
} 
The standard says that sizeof(int) must be at least as big as sizeof(char), so that one is OK

int main()
{
	static_assertion<sizeof(char) > sizeof(int)>();
} 
Something which can never possibly be true, sizeof(char) being greater than sizeof(int), results in a big ugly error message. Not ideal, but a zillion times better than allowing an ill-formed bit of code past the compiler.

Boost's implementation of static_assert uses some funky macro wizardry to allow better compiler error messages

This post has been edited by Bench: 12 November 2009 - 10:31 AM

Was This Post Helpful? 0
  • +
  • -

#7 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 844
  • View blog
  • Posts: 2,334
  • Joined: 20-August 07

Re: Limits on function parameters

Posted 12 November 2009 - 10:37 AM

View PostEdwinNameless, on 12 Nov, 2009 - 04:09 PM, said:

I'm glad I wasn't totally off the mark in my response, then! ;)

On another note, interesting use of templates which I had never seen. Seems pretty drastic, though: is it something commonly used? As far as I'm concerned, I'd tend to use the assertion way, but I'm wondering if it's my heavy Java background that's doing the talk again...

The Boost libraries are full of such examples, as are the C++ standard libraries, and also Andrei Alexandrescu's 'Loki' library. Java's 'generics' are far less powerful than C++'s templates; They are unable to support this kind of meta-programming
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1