initializing to expressions

Stephen Clamage steve at taumet.com
Sun Feb 10 05:16:46 AEST 1991


BRL102 at psuvm.psu.edu (Ben Liblit) writes:

>I want to initialize an array [statically] with values that are constant,
>but are in the form of expressions.  For example,

>   double  value[ 2 ] = { sqrt( 2 ) / 2, cos ( sqrt( 5 ) ) };

>Is there any way to accomplish this?

In C, static initializers may not contain function calls.  sqrt and cos
are part of the run-time library, not built-in to the language.

C++, however, allows general expressions to initialize static variables.
The compiler collects non-constant expressions into a compiler-generated
initialization routine, then calls the routine before any user code in
the main program.  This is in fact run-time, rather than compile-time,
initialization, but it allows the convenient notation you want.
-- 

Steve Clamage, TauMetric Corp, steve at taumet.com



More information about the Comp.lang.c mailing list