User defined compile-time error

  • Thread starter Alexander Dong Back Kim
  • Start date
A

Alexander Dong Back Kim

Dear all,

I'm just wondering whether I can define a compile-time error for a
class
or a function when the parameters are wrong.

For example,

class Test
{
protected :
int _value;
int _max;
int _min;

public :
Test();
Test(int v, int max, int min);
};

Test::Test() : _value(0), _max(0), _min(0) { }

Test::Test(int v, int max, int min)
{
_value = v;

if (max < min || v > max || v < min)
{
// throwing an error
}
else
{
_value = v; _max = max; _min = min;
}
}

It would be really nice that if the "throwing an error" actually
causes
an compile-time error with nice error message something like
"ERROR : Test::Test(int v, int man, int min) assigned wrong
parameters"... but I have no idea how do this and I'm even not sure
whether this approach is allowed or not.

any suggestion or trick? =)

regards,
Alex Kim
 
A

Alexander Dong Back Kim

Dear all,

I'm just wondering whether I can define a compile-time error for a
class
or a function when the parameters are wrong.

For example,

class Test
{
protected :
int _value;
int _max;
int _min;

public :
Test();
Test(int v, int max, int min);

};

Test::Test() : _value(0), _max(0), _min(0) { }

Test::Test(int v, int max, int min)
{
_value = v;

if (max < min || v > max || v < min)
{
// throwing an error
}
else
{
_value = v; _max = max; _min = min;
}

}

It would be really nice that if the "throwing an error" actually
causes
an compile-time error with nice error message something like
"ERROR : Test::Test(int v, int man, int min) assigned wrong
parameters"... but I have no idea how do this and I'm even not sure
whether this approach is allowed or not.

any suggestion or trick? =)

regards,
Alex Kim

Hum... never mind. This was a ridiculous questions =P

cheers,
Alex Kim
 
I

Ian Collins

Alexander said:
Dear all,

I'm just wondering whether I can define a compile-time error for a
class
or a function when the parameters are wrong.
Test::Test(int v, int max, int min)
{
_value = v;

if (max < min || v > max || v < min)
{
// throwing an error
}
else
{
_value = v; _max = max; _min = min;
}
}

It would be really nice that if the "throwing an error" actually
causes
an compile-time error with nice error message something like
"ERROR : Test::Test(int v, int man, int min) assigned wrong
parameters"... but I have no idea how do this and I'm even not sure
whether this approach is allowed or not.
Not so daft if you really can test at compile time, how about:

template <int V, int Min, int Max>
struct Test
{
enum { a = (Max > Min), b = (V > Min), c = (V <Max) };

static const unsigned t1 = 1/a;
static const unsigned t2 = 1/b;
static const unsigned t3 = 1/c;
};

int main()
{
Test<0, 4, 3> t;
}
 
A

Alexander Dong Back Kim

Not so daft if you really can test at compile time, how about:

template <int V, int Min, int Max>
struct Test
{
enum { a = (Max > Min), b = (V > Min), c = (V <Max) };

static const unsigned t1 = 1/a;
static const unsigned t2 = 1/b;
static const unsigned t3 = 1/c;

};

int main()
{
Test<0, 4, 3> t;

}

Dear Ian,

thanks for your suggestion. However, I can't understand how I can
apply your codes to mine. Only way I can think of making user-defined
compile-time error may be using preprocessor which is "#warning and
#error". Note that this is possible for preprocessor only so it is not
the one that I'm looking for. Therefore, I think generating a user-
defined compile-time error is impossible.

For example,

#ifndef VALUE
#error "Value hasn't been defined!"
#endif

This one will show an error during the compile-time. However, we can
make the function like the following...

void check (int max, int min)
{
if ( max < min )
{
#error "max is smaller than min"
}
}

Whatever the if statement's evaluation result is it will always
causing an error. One thing I missed was max and min is parameters.
They are variables so there is no way preprocessor can know the actual
value of the parameters during the compile-time. It will be anything
during the run-time.

cheers,
Alex Kim
 
I

Ian Collins

*Please* don't quote signatres.
Dear Ian,

thanks for your suggestion. However, I can't understand how I can
apply your codes to mine.

Well you dis ask for a compile time solution!
Only way I can think of making user-defined
compile-time error may be using preprocessor which is "#warning and
#error". Note that this is possible for preprocessor only so it is not
the one that I'm looking for. Therefore, I think generating a user-
defined compile-time error is impossible.
In this context, yes it is impossible.
 
A

Alexander Dong Back Kim

*Please* don't quote signatres.





Well you dis ask for a compile time solution!


In this context, yes it is impossible.

Thanks =)

cheers,
Alex Kim
 
P

PeterAPIIT

Unless, you can develop a plugin using adapter pattern to work with
existing compiler.

I may wrong.

Please correct me if i wrong.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,814
Latest member
SpicetreeDigital

Latest Threads

Top