Topic: proposal: use of CONST for deterministic functions
Author: "brianchon" <brianchon@sina.com.--->
Date: Sun, 10 Mar 2002 20:46:08 GMT Raw View
IMHO, had this feature supported, the compiler should try to evaluate all
the functions declared 'CONST' when they appear in the place of a constant
definition. However, there's no way to tell if a function really terminates,
so the compiler itself may not terminate. This feature is better not in the
standard.
brianchon
> Instead, suppose I could write (the syntax is just a proposal, don't focus
> on the details!):
>
>
>
> int SDF(int x) const
> {
> return 27*x+16;
> }
>
>
> enum
> {
> FIRST = SDF(34), // accepted!!!
> SECOND = SDF(67)
> }
>
>
>
> --
> The set of solutions is never empty.
> Two solutions together form a new problem.
> -- Mycroft Holmes
>
> ---
> [ 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 ]
>
---
[ 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: Ron Natalie <ron@sensor.com>
Date: Mon, 4 Feb 2002 16:06:51 GMT Raw View
Mycroft Holmes wrote:
>
> Hi to all.
>
> I'm writing here a simple example which nowadays is illegal: suppose you
> have a global function, which is deterministic, i.e. calling it twice does
> give the same result (this is the case for most math functions).\\
Determinism is only part of the issue. The bigger part is how far it can
be allowed to force the compiler to be able to evaluate at compile time
when you extend the definition of constant exprssions.
Frankly if you wish to do this, there's no point at all in adding the const
declaration. The compiler is perfectly able to reject things that don't fit
the definition of a constant expression as it does now.
---
[ 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: Florian Kaufmann <sensorflo@bigfoot.com>
Date: Mon, 4 Feb 2002 16:07:54 GMT Raw View
Hi
You want to have a function, whichs return value to a give parameter is
already known at compile time, is that what you have in mind? If yes, use
macros like:
#define SLOPE( x, b, m ) ( (m) * (x) + (b) )
The precompiler ( which runs before the actual compiler ), replaces SLOPE
with the given formula.
After the precompiler, the following fragment
int x = 3, y;
y = SLOPE( x , 3+3, 5 );
lookes like this
int x = 3, y;
y = ( (3) * (x) + (5) );
Which can is interpreted at compile time. Dont forget the many parethesis.
Else
#define SLOPE(x,b,m) m*x+b
y = SLOPE( x , k + 3, 5 );
becomes to
y = k + 3 * x + b;
Which is not what you intended.
Greetings
Sensorflo
This works also for recursive functions like
#define FIBO(n) ( ( (n) == 1 ) ? 1 : ( ((n)==2) ? 1 : ( FIBO((n)-2) +
FIBO((n)-1) ) )
Mycroft Holmes wrote:
> Hi to all.
>
> I'm writing here a simple example which nowadays is illegal: suppose you
> have a global function, which is deterministic, i.e. calling it twice does
> give the same result (this is the case for most math functions).
> C++ does not offer a keyword to declare a function as deterministic; so the
> compiler can't use the function to build a compile-time constant.
>
> Here's an example:
>
> int SDF(int x) // Some Deterministic Function
> {
> return 27*x+16;
> }
>
> const int result1 = SDF(35); // OK
> const int result2 = SDF(46); // OK
>
> int main()
> {
> int i;
>
> //....
>
> switch (i)
> {
> case result1: // ERROR
> do_something();
> }
>
> }
>
> Instead, suppose I could write (the syntax is just a proposal, don't focus
> on the details!):
>
> int SDF(int x) const
> {
> return 27*x+16;
> }
>
> enum
> {
> FIRST = SDF(34), // accepted!!!
> SECOND = SDF(67)
> }
>
`
---
[ 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: "Harri" <no@email.sorry>
Date: Mon, 4 Feb 2002 18:51:22 GMT Raw View
"Mycroft Holmes" <holmes@technologist.REMOVEME.com> wrote
[ some trimming ]
> I'm writing here a simple example which nowadays is illegal: suppose you
> have a global function, which is deterministic, i.e. calling it twice does
> give the same result (this is the case for most math functions).
> C++ does not offer a keyword to declare a function as deterministic; so
the
> compiler can't use the function to build a compile-time constant.
>
> Here's an example:
>
> int SDF(int x) // Some Deterministic Function
> {
> return 27*x+16;
> }
>
> const int result1 = SDF(35); // OK
> const int result2 = SDF(46); // OK
>
> int main()
> {
> int i;
> //....
> switch (i)
> {
> case result1: // ERROR
> do_something();
> }
> }
You can already write something like
template<int I>
struct SDF{
enum { value = 27 * I +16 };
};
const int result1 = SDF<35>::value;
const int result2 = SDF<46>::value;
switch (i)
{
case result1:
do_something();
}
The syntax is less than intuitive to people not familiar with template
programming, but the facility is there.
Harri.
---
[ 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: "Mycroft Holmes" <holmes@technologist.REMOVEME.com>
Date: Mon, 4 Feb 2002 04:14:58 CST Raw View
Hi to all.
I'm writing here a simple example which nowadays is illegal: suppose you
have a global function, which is deterministic, i.e. calling it twice does
give the same result (this is the case for most math functions).
C++ does not offer a keyword to declare a function as deterministic; so the
compiler can't use the function to build a compile-time constant.
Here's an example:
int SDF(int x) // Some Deterministic Function
{
return 27*x+16;
}
const int result1 = SDF(35); // OK
const int result2 = SDF(46); // OK
int main()
{
int i;
//....
switch (i)
{
case result1: // ERROR
do_something();
}
}
Instead, suppose I could write (the syntax is just a proposal, don't focus
on the details!):
int SDF(int x) const
{
return 27*x+16;
}
enum
{
FIRST = SDF(34), // accepted!!!
SECOND = SDF(67)
}
--
The set of solutions is never empty.
Two solutions together form a new problem.
-- Mycroft Holmes
---
[ 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 ]