Changeset f988834 for doc/user/user.tex


Ignore:
Timestamp:
Jan 19, 2024, 2:44:41 AM (20 months ago)
Author:
JiadaL <j82liang@…>
Branches:
master
Children:
ac939461
Parents:
59c8dff (diff), e8b3717 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'master' of plg.uwaterloo.ca:software/cfa/cfa-cc

File:
1 edited

Legend:

Unmodified
Added
Removed
  • doc/user/user.tex

    r59c8dff rf988834  
    1111%% Created On       : Wed Apr  6 14:53:29 2016
    1212%% Last Modified By : Peter A. Buhr
    13 %% Last Modified On : Sat Sep 30 22:46:19 2023
    14 %% Update Count     : 5658
     13%% Last Modified On : Sun Jan 14 17:27:41 2024
     14%% Update Count     : 5764
    1515%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    1616
     
    5555\SetWatermarkLightness{0.9}
    5656
    57 % Default underscore is too low and wide. Cannot use lstlisting "literate" as replacing underscore
    58 % removes it as a variable-name character so keywords in variables are highlighted. MUST APPEAR
    59 % AFTER HYPERREF.
    60 \renewcommand{\textunderscore}{\leavevmode\makebox[1.2ex][c]{\rule{1ex}{0.075ex}}}
     57% Default underscore is too low. Cannot use lstlisting "literate" as replacing underscore removes it
     58% as a variable-name character so keywords in variables are highlighted. MUST APPEAR AFTER HYPERREF.
     59\renewcommand{\textunderscore}{\makebox[1.4ex][c]{{\raisebox{1.25pt}{\char`\_}}}}
    6160
    6261\setlength{\topmargin}{-0.45in}                                                 % move running title into header
     
    6766\CFAStyle                                                                                               % use default CFA format-style
    6867\setgcolumn{2.25in}
    69 %\lstset{language=CFA}                                                                  % CFA default lnaguage
     68\lstset{language=CFA}                                                                   % CFA default lnaguage
    7069\lstnewenvironment{C++}[1][]                            % use C++ style
    7170{\lstset{language=C++,moredelim=**[is][\protect\color{red}]{®}{®},#1}}
     
    172171\begin{tabular}{@{}lll@{}}
    173172\multicolumn{1}{@{}c}{\textbf{C}}       & \multicolumn{1}{c}{\textbf{\CFA}}     & \multicolumn{1}{c@{}}{\textbf{\CC}}   \\
    174 \begin{cfa}[tabsize=3]
    175 #include <stdio.h>$\indexc{stdio.h}$
     173\begin{cfa}
     174#include <stdio.h>§\indexc{stdio.h}§
    176175
    177176int main( void ) {
     
    182181&
    183182\begin{cfa}[tabsize=3]
    184 #include <fstream>$\indexc{fstream}$
     183#include <fstream>§\indexc{fstream}§
    185184
    186185int main( void ) {
    187186        int x = 0, y = 1, z = 2;
    188         ®sout | x | y | z;®$\indexc{sout}$
     187        ®sout | x | y | z;®§\indexc{sout}§
    189188}
    190189\end{cfa}
    191190&
    192191\begin{cfa}[tabsize=3]
    193 #include <iostream>$\indexc{iostream}$
     192#include <iostream>§\indexc{iostream}§
    194193using namespace std;
    195194int main() {
     
    260259\begin{cfa}
    261260®forall( T )® T identity( T val ) { return val; }
    262 int forty_two = identity( 42 ); $\C{// T is bound to int, forty\_two == 42}$
     261int forty_two = identity( 42 ); §\C{// T is bound to int, forty\_two == 42}§
    263262\end{cfa}
    264263% extending the C type system with parametric polymorphism and overloading, as opposed to the \Index*[C++]{\CC{}} approach of object-oriented extensions.
     
    288287
    289288double key = 5.0, vals[10] = { /* 10 sorted floating values */ };
    290 double * val = (double *)bsearch( &key, vals, 10, sizeof(vals[0]), comp ); $\C{// search sorted array}$
     289double * val = (double *)bsearch( &key, vals, 10, sizeof(vals[0]), comp ); §\C{// search sorted array}§
    291290\end{cfa}
    292291which can be augmented simply with a polymorphic, type-safe, \CFA-overloaded wrappers:
     
    297296
    298297forall( T | { int ?<?( T, T ); } ) unsigned int bsearch( T key, const T * arr, size_t size ) {
    299         T * result = bsearch( key, arr, size ); $\C{// call first version}$
    300         return result ? result - arr : size; } $\C{// pointer subtraction includes sizeof(T)}$
    301 
    302 double * val = bsearch( 5.0, vals, 10 ); $\C{// selection based on return type}$
     298        T * result = bsearch( key, arr, size ); §\C{// call first version}§
     299        return result ? result - arr : size; } §\C{// pointer subtraction includes sizeof(T)}§
     300
     301double * val = bsearch( 5.0, vals, 10 ); §\C{// selection based on return type}§
    303302int posn = bsearch( 5.0, vals, 10 );
    304303\end{cfa}
     
    312311\begin{cfa}
    313312forall( dtype T | sized(T) ) T * malloc( void ) { return (T *)malloc( sizeof(T) ); }
    314 int * ip = malloc(); $\C{// select type and size from left-hand side}$
     313int * ip = malloc(); §\C{// select type and size from left-hand side}§
    315314double * dp = malloc();
    316315struct S {...} * sp = malloc();
     
    324323\begin{cfa}
    325324char ®abs®( char );
    326 extern "C" { int ®abs®( int ); } $\C{// use default C routine for int}$
     325extern "C" { int ®abs®( int ); } §\C{// use default C routine for int}§
    327326long int ®abs®( long int );
    328327long long int ®abs®( long long int );
     
    349348The command ©cfa© is used to compile a \CFA program and is based on the \Index{GNU} \Indexc{gcc} command, \eg:
    350349\begin{cfa}
    351 cfa$\indexc{cfa}\index{compilation!cfa@©cfa©}$ [ gcc/$\CFA{}$-options ] [ C/$\CFA{}$ source-files ] [ assembler/loader files ]
     350cfa§\indexc{cfa}\index{compilation!cfa@©cfa©}§ [ gcc/§\CFA{}§-options ] [ C/§\CFA{}§ source-files ] [ assembler/loader files ]
    352351\end{cfa}
    353352There is no ordering among options (flags) and files, unless an option has an argument, which must appear immediately after the option possibly with or without a space separating option and argument.
     
    438437\begin{cfa}
    439438#ifndef __CFORALL__
    440 #include <stdio.h>$\indexc{stdio.h}$ $\C{// C header file}$
     439#include <stdio.h>§\indexc{stdio.h}§ §\C{// C header file}§
    441440#else
    442 #include <fstream>$\indexc{fstream}$ $\C{// \CFA header file}$
     441#include <fstream>§\indexc{fstream}§ §\C{// \CFA header file}§
    443442#endif
    444443\end{cfa}
     
    450449Each option must be escaped with \Indexc{-XCFA}\index{translator option!-XCFA@{©-XCFA©}} to direct it to the compiler step, similar to the ©-Xlinker© flag for the linker, \eg:
    451450\begin{lstlisting}[language=sh]
    452 cfa $test$.cfa -CFA -XCFA -p # print translated code without printing the standard prelude
    453 cfa $test$.cfa -XCFA -P -XCFA parse -XCFA -n # show program parse without prelude
     451cfa §test§.cfa -CFA -XCFA -p # print translated code without printing the standard prelude
     452cfa §test§.cfa -XCFA -P -XCFA parse -XCFA -n # show program parse without prelude
    454453\end{lstlisting}
    455 Alternatively, multiple flages can be specified separated with commas and \emph{without} spaces.
     454Alternatively, multiple flags can be specified separated with commas and \emph{without} spaces.
    456455\begin{lstlisting}[language=sh,{moredelim=**[is][\protect\color{red}]{®}{®}}]
    457 cfa $test$.cfa -XCFA®,®-Pparse®,®-n # show program parse without prelude
     456cfa §test§.cfa -XCFA®,®-Pparse®,®-n # show program parse without prelude
    458457\end{lstlisting}
    459458\begin{description}[topsep=5pt,itemsep=0pt,parsep=0pt]
     
    537536Keyword clashes are accommodated by syntactic transformations using the \CFA backquote escape-mechanism:
    538537\begin{cfa}
    539 int ®``®coroutine = 3;                                  $\C{// make keyword an identifier}$
     538int ®``®coroutine = 3;                                  §\C{// make keyword an identifier}§
    540539double ®``®forall = 3.5;
    541540\end{cfa}
     
    547546\begin{cfa}
    548547// include file uses the CFA keyword "with".
    549 #if ! defined( with )                                   $\C{// nesting ?}$
    550 #define with ®``®with                                   $\C{// make keyword an identifier}$
     548#if ! defined( with )                                   §\C{// nesting ?}§
     549#define with ®``®with                                   §\C{// make keyword an identifier}§
    551550#define __CFA_BFD_H__
    552551#endif
    553 $\R{\#include\_next} <bfdlink.h>$               $\C{// must have internal check for multiple expansion}$
    554 #if defined( with ) && defined( __CFA_BFD_H__ ) $\C{// reset only if set}$
     552§\R{\#include\_next} <bfdlink.h>§               §\C{// must have internal check for multiple expansion}§
     553#if defined( with ) && defined( __CFA_BFD_H__ ) §\C{// reset only if set}§
    555554#undef with
    556555#undef __CFA_BFD_H__
     
    566565Numeric constants are extended to allow \Index{underscore}s\index{constant!underscore} as a separator, \eg:
    567566\begin{cfa}
    568 2®_®147®_®483®_®648;                                    $\C{// decimal constant}$
    569 56®_®ul;                                                                $\C{// decimal unsigned long constant}$
    570 0®_®377;                                                                $\C{// octal constant}$
    571 0x®_®ff®_®ff;                                                   $\C{// hexadecimal constant}$
    572 0x®_®ef3d®_®aa5c;                                               $\C{// hexadecimal constant}$
    573 3.141®_®592®_®654;                                              $\C{// floating constant}$
    574 10®_®e®_®+1®_®00;                                               $\C{// floating constant}$
    575 0x®_®ff®_®ff®_®p®_®3;                                   $\C{// hexadecimal floating}$
    576 0x®_®1.ffff®_®ffff®_®p®_®128®_®l;               $\C{// hexadecimal floating long constant}$
    577 L®_®$"\texttt{\textbackslash{x}}$®_®$\texttt{ff}$®_®$\texttt{ee}"$; $\C{// wide character constant}$
     5672®_®147®_®483®_®648;                                    §\C{// decimal constant}§
     56856®_®ul;                                                                §\C{// decimal unsigned long constant}§
     5690®_®377;                                                                §\C{// octal constant}§
     5700x®_®ff®_®ff;                                                   §\C{// hexadecimal constant}§
     5710x®_®ef3d®_®aa5c;                                               §\C{// hexadecimal constant}§
     5723.141®_®592®_®654;                                              §\C{// floating constant}§
     57310®_®e®_®+1®_®00;                                               §\C{// floating constant}§
     5740x®_®ff®_®ff®_®p®_®3;                                   §\C{// hexadecimal floating}§
     5750x®_®1.ffff®_®ffff®_®p®_®128®_®l;               §\C{// hexadecimal floating long constant}§
     576L®_®§"\texttt{\textbackslash{x}}§®_®§\texttt{ff}§®_®§\texttt{ee}"§; §\C{// wide character constant}§
    578577\end{cfa}
    579578The rules for placement of underscores are:
     
    635634Declarations in the \Indexc{do}-©while© condition are not useful because they appear after the loop body.}
    636635\begin{cfa}
    637 if ( ®int x = f()® ) ...                        $\C{// x != 0}$
    638 if ( ®int x = f(), y = g()® ) ...       $\C{// x != 0 \&\& y != 0}$
    639 if ( ®int x = f(), y = g(); x < y® ) ... $\C{// relational expression}$
    640 if ( ®struct S { int i; } x = { f() }; x.i < 4® ) $\C{// relational expression}$
    641 
    642 while ( ®int x = f()® ) ...                     $\C{// x != 0}$
    643 while ( ®int x = f(), y = g()® ) ... $\C{// x != 0 \&\& y != 0}$
    644 while ( ®int x = f(), y = g(); x < y® ) ... $\C{// relational expression}$
    645 while ( ®struct S { int i; } x = { f() }; x.i < 4® ) ... $\C{// relational expression}$
     636if ( ®int x = f()® ) ...                        §\C{// x != 0}§
     637if ( ®int x = f(), y = g()® ) ...       §\C{// x != 0 \&\& y != 0}§
     638if ( ®int x = f(), y = g(); x < y® ) ... §\C{// relational expression}§
     639if ( ®struct S { int i; } x = { f() }; x.i < 4® ) §\C{// relational expression}§
     640
     641while ( ®int x = f()® ) ...                     §\C{// x != 0}§
     642while ( ®int x = f(), y = g()® ) ... §\C{// x != 0 \&\& y != 0}§
     643while ( ®int x = f(), y = g(); x < y® ) ... §\C{// relational expression}§
     644while ( ®struct S { int i; } x = { f() }; x.i < 4® ) ... §\C{// relational expression}§
    646645\end{cfa}
    647646Unless a relational expression is specified, each variable is compared not equal to 0, which is the standard semantics for the ©if©/©while© expression, and the results are combined using the logical \Indexc{&&} operator.
     
    713712\begin{cfa}
    714713switch ( i ) {
    715   case 1$\R{\textvisiblespace}$®...®4:
     714  case 1§\R{\textvisiblespace}§®...®4:
    716715        ...
    717   case 10$\R{\textvisiblespace}$®...®13:
     716  case 10§\R{\textvisiblespace}§®...®13:
    718717        ...
    719718}
     
    752751  case 1:
    753752        ...
    754         $\R{\LstCommentStyle{// fall-through}}$
     753        §\R{\LstCommentStyle{// fall-through}}§
    755754  case 2:
    756755        ...
     
    853852\begin{cfa}
    854853switch ( x ) {
    855         ®int y = 1;® $\C{// unreachable initialization}$
    856         ®x = 7;® $\C{// unreachable code without label/branch}$
     854        ®int y = 1;® §\C{// unreachable initialization}§
     855        ®x = 7;® §\C{// unreachable code without label/branch}§
    857856  case 0: ...
    858857        ...
    859         ®int z = 0;® $\C{// unreachable initialization, cannot appear after case}$
     858        ®int z = 0;® §\C{// unreachable initialization, cannot appear after case}§
    860859        z = 2;
    861860  case 1:
    862         ®x = z;® $\C{// without fall through, z is uninitialized}$
     861        ®x = z;® §\C{// without fall through, z is uninitialized}§
    863862}
    864863\end{cfa}
     
    895894  case 1:  case 2:  case 3:
    896895        ...
    897         $\R{\LstCommentStyle{// implicit end of switch (break)}}$
     896        §\R{\LstCommentStyle{// implicit end of switch (break)}}§
    898897  case 5:
    899898        ...
    900         ®fallthru®; $\C{// explicit fall through}$
     899        ®fallthru®; §\C{// explicit fall through}§
    901900  case 7:
    902901        ...
    903         ®break® $\C{// explicit end of switch (redundant)}$
     902        ®break® §\C{// explicit end of switch (redundant)}§
    904903  default:
    905904        j = 3;
     
    922921\begin{cfa}
    923922switch ( x ) {
    924         ®int i = 0;®                                            $\C{// allowed only at start}$
     923        ®int i = 0;®                                            §\C{// allowed only at start}§
    925924  case 0:
    926925        ...
    927         ®int j = 0;®                                            $\C{// disallowed}$
     926        ®int j = 0;®                                            §\C{// disallowed}§
    928927  case 1:
    929928        {
    930                 ®int k = 0;®                                    $\C{// allowed at different nesting levels}$
     929                ®int k = 0;®                                    §\C{// allowed at different nesting levels}§
    931930                ...
    932           ®case 2:®                                                     $\C{// disallow case in nested statements}$
     931          ®case 2:®                                                     §\C{// disallow case in nested statements}§
    933932        }
    934933  ...
     
    10041003while () { sout | "empty"; break; }
    10051004do { sout | "empty"; break; } while ();
    1006 for () { sout | "empty"; break; }                                                       $\C[3in]{sout | nl | nlOff;}$
    1007 
    1008 for ( 0 ) { sout | "A"; } sout | "zero";                                        $\C{sout | nl;}$
    1009 for ( 1 ) { sout | "A"; }                                                                       $\C{sout | nl;}$
    1010 for ( 10 ) { sout | "A"; }                                                                      $\C{sout | nl;}$
    1011 for ( ~= 10 ) { sout | "A"; }                                                           $\C{sout | nl;}$
    1012 for ( 1 ~= 10 ~ 2 ) { sout | "B"; }                                                     $\C{sout | nl;}$
    1013 for ( 1 -~= 10 ~ 2 ) { sout | "C"; }                                            $\C{sout | nl;}$
    1014 for ( 0.5 ~ 5.5 ) { sout | "D"; }                                                       $\C{sout | nl;}$
    1015 for ( 0.5 -~ 5.5 ) { sout | "E"; }                                                      $\C{sout | nl;}$
    1016 for ( i; 10 ) { sout | i; }                                                                     $\C{sout | nl;}$
    1017 for ( i; ~= 10 ) { sout | i; }                                                          $\C{sout | nl;}$
    1018 for ( i; 1 ~= 10 ~ 2 ) { sout | i; }                                            $\C{sout | nl;}$
    1019 for ( i; 1 -~= 10 ~ 2 ) { sout | i; }                                           $\C{sout | nl;}$
    1020 for ( i; 0.5 ~ 5.5 ) { sout | i; }                                                      $\C{sout | nl;}$
    1021 for ( i; 0.5 -~ 5.5 ) { sout | i; }                                                     $\C{sout | nl;}$
    1022 for ( ui; 2u ~= 10u ~ 2u ) { sout | ui; }                                       $\C{sout | nl;}$
    1023 for ( ui; 2u -~= 10u ~ 2u ) { sout | ui; }                                      $\C{sout | nl | nl | nl;}$
     1005for () { sout | "empty"; break; }                                                       §\C[3in]{sout | nl | nlOff;}§
     1006
     1007for ( 0 ) { sout | "A"; } sout | "zero";                                        §\C{sout | nl;}§
     1008for ( 1 ) { sout | "A"; }                                                                       §\C{sout | nl;}§
     1009for ( 10 ) { sout | "A"; }                                                                      §\C{sout | nl;}§
     1010for ( ~= 10 ) { sout | "A"; }                                                           §\C{sout | nl;}§
     1011for ( 1 ~= 10 ~ 2 ) { sout | "B"; }                                                     §\C{sout | nl;}§
     1012for ( 1 -~= 10 ~ 2 ) { sout | "C"; }                                            §\C{sout | nl;}§
     1013for ( 0.5 ~ 5.5 ) { sout | "D"; }                                                       §\C{sout | nl;}§
     1014for ( 0.5 -~ 5.5 ) { sout | "E"; }                                                      §\C{sout | nl;}§
     1015for ( i; 10 ) { sout | i; }                                                                     §\C{sout | nl;}§
     1016for ( i; ~= 10 ) { sout | i; }                                                          §\C{sout | nl;}§
     1017for ( i; 1 ~= 10 ~ 2 ) { sout | i; }                                            §\C{sout | nl;}§
     1018for ( i; 1 -~= 10 ~ 2 ) { sout | i; }                                           §\C{sout | nl;}§
     1019for ( i; 0.5 ~ 5.5 ) { sout | i; }                                                      §\C{sout | nl;}§
     1020for ( i; 0.5 -~ 5.5 ) { sout | i; }                                                     §\C{sout | nl;}§
     1021for ( ui; 2u ~= 10u ~ 2u ) { sout | ui; }                                       §\C{sout | nl;}§
     1022for ( ui; 2u -~= 10u ~ 2u ) { sout | ui; }                                      §\C{sout | nl | nl | nl;}§
    10241023
    10251024enum { N = 10 };
    1026 for ( N ) { sout | "N"; }                                                                       $\C{sout | nl;}$
    1027 for ( i; N ) { sout | i; }                                                                      $\C{sout | nl;}$
    1028 for ( i; -~ N ) { sout | i; }                                                           $\C{sout | nl | nl | nl;}$
     1025for ( N ) { sout | "N"; }                                                                       §\C{sout | nl;}§
     1026for ( i; N ) { sout | i; }                                                                      §\C{sout | nl;}§
     1027for ( i; -~ N ) { sout | i; }                                                           §\C{sout | nl | nl | nl;}§
    10291028
    10301029const int low = 3, high = 10, inc = 2;
    1031 for ( i; low ~ high ~ inc + 1 ) { sout | i; }                           $\C{sout | nl;}$
    1032 for ( i; 1 ~ @ ) { if ( i > 10 ) break; sout | i; }                     $\C{sout | nl;}$
    1033 for ( i; @ -~ 10 ) { if ( i < 0 ) break; sout | i; }            $\C{sout | nl;}$
    1034 for ( i; 2 ~ @ ~ 2 ) { if ( i > 10 ) break; sout | i; }         $\C{sout | nl;}$
    1035 for ( i; 2.1 ~ @ ~ @ ) { if ( i > 10.5 ) break; sout | i; i += 1.7; } $\C{sout | nl;}$
    1036 for ( i; @ -~ 10 ~ 2 ) { if ( i < 0 ) break; sout | i; }        $\C{sout | nl;}$
    1037 for ( i; 12.1 ~ @ ~ @ ) { if ( i < 2.5 ) break; sout | i; i -= 1.7; } $\C{sout | nl;}$
    1038 for ( i; 5 : j; -5 ~ @ ) { sout | i | j; }                                      $\C{sout | nl;}$
    1039 for ( i; 5 : j; @ -~ -5 ) { sout | i | j; }                                     $\C{sout | nl;}$
    1040 for ( i; 5 : j; -5 ~ @ ~ 2 ) { sout | i | j; }                          $\C{sout | nl;}$
    1041 for ( i; 5 : j; @ -~ -5 ~ 2 ) { sout | i | j; }                         $\C{sout | nl;}$
    1042 for ( i; 5 : j; -5 ~ @ ) { sout | i | j; }                                      $\C{sout | nl;}$
    1043 for ( i; 5 : j; @ -~ -5 ) { sout | i | j; }                                     $\C{sout | nl;}$
    1044 for ( i; 5 : j; -5 ~ @ ~ 2 ) { sout | i | j; }                          $\C{sout | nl;}$
    1045 for ( i; 5 : j; @ -~ -5 ~ 2 ) { sout | i | j; }                         $\C{sout | nl;}$
    1046 for ( i; 5 : j; @ -~ -5 ~ 2 : k; 1.5 ~ @ ) { sout | i | j | k; } $\C{sout | nl;}$
    1047 for ( i; 5 : j; @ -~ -5 ~ 2 : k; 1.5 ~ @ ) { sout | i | j | k; } $\C{sout | nl;}$
    1048 for ( i; 5 : k; 1.5 ~ @ : j; @ -~ -5 ~ 2 ) { sout | i | j | k; } $\C{sout | nl;}\CRT$
     1030for ( i; low ~ high ~ inc + 1 ) { sout | i; }                           §\C{sout | nl;}§
     1031for ( i; 1 ~ @ ) { if ( i > 10 ) break; sout | i; }                     §\C{sout | nl;}§
     1032for ( i; @ -~ 10 ) { if ( i < 0 ) break; sout | i; }            §\C{sout | nl;}§
     1033for ( i; 2 ~ @ ~ 2 ) { if ( i > 10 ) break; sout | i; }         §\C{sout | nl;}§
     1034for ( i; 2.1 ~ @ ~ @ ) { if ( i > 10.5 ) break; sout | i; i += 1.7; } §\C{sout | nl;}§
     1035for ( i; @ -~ 10 ~ 2 ) { if ( i < 0 ) break; sout | i; }        §\C{sout | nl;}§
     1036for ( i; 12.1 ~ @ ~ @ ) { if ( i < 2.5 ) break; sout | i; i -= 1.7; } §\C{sout | nl;}§
     1037for ( i; 5 : j; -5 ~ @ ) { sout | i | j; }                                      §\C{sout | nl;}§
     1038for ( i; 5 : j; @ -~ -5 ) { sout | i | j; }                                     §\C{sout | nl;}§
     1039for ( i; 5 : j; -5 ~ @ ~ 2 ) { sout | i | j; }                          §\C{sout | nl;}§
     1040for ( i; 5 : j; @ -~ -5 ~ 2 ) { sout | i | j; }                         §\C{sout | nl;}§
     1041for ( i; 5 : j; -5 ~ @ ) { sout | i | j; }                                      §\C{sout | nl;}§
     1042for ( i; 5 : j; @ -~ -5 ) { sout | i | j; }                                     §\C{sout | nl;}§
     1043for ( i; 5 : j; -5 ~ @ ~ 2 ) { sout | i | j; }                          §\C{sout | nl;}§
     1044for ( i; 5 : j; @ -~ -5 ~ 2 ) { sout | i | j; }                         §\C{sout | nl;}§
     1045for ( i; 5 : j; @ -~ -5 ~ 2 : k; 1.5 ~ @ ) { sout | i | j | k; } §\C{sout | nl;}§
     1046for ( i; 5 : j; @ -~ -5 ~ 2 : k; 1.5 ~ @ ) { sout | i | j | k; } §\C{sout | nl;}§
     1047for ( i; 5 : k; 1.5 ~ @ : j; @ -~ -5 ~ 2 ) { sout | i | j | k; } §\C{sout | nl;}\CRT§
    10491048\end{cfa}
    10501049&
     
    11171116The \Indexc{for}, \Indexc{while}, and \Indexc{do} loop-control allow an empty conditional, which implies a comparison value of ©1© (true).
    11181117\begin{cfa}
    1119 while ( ®/* empty */®  )                                $\C{// while ( true )}$
    1120 for ( ®/* empty */®  )                                  $\C{// for ( ; true; )}$
    1121 do ... while ( ®/* empty */®  )                 $\C{// do ... while ( true )}$
     1118while ( ®/* empty */®  )                                §\C{// while ( true )}§
     1119for ( ®/* empty */®  )                                  §\C{// for ( ; true; )}§
     1120do ... while ( ®/* empty */®  )                 §\C{// do ... while ( true )}§
    11221121\end{cfa}
    11231122
     
    11491148If no type is specified for the loop index, it is the type of the high value H (when the low value is implicit) or the low value L.
    11501149\begin{cfa}
    1151 for ( ®5® )                                                             $\C{// typeof(5) anonymous-index; 5 is high value}$
    1152 for ( i; ®1.5® ~ 5.5 )                                  $\C{// typeof(1.5) i; 1.5 is low value}$
    1153 for ( ®int i®; 0 ~ 10 ~ 2 )                             $\C{// int i; type is explicit}$
     1150for ( ®5® )                                                             §\C{// typeof(5) anonymous-index; 5 is high value}§
     1151for ( i; ®1.5® ~ 5.5 )                                  §\C{// typeof(1.5) i; 1.5 is low value}§
     1152for ( ®int i®; 0 ~ 10 ~ 2 )                             §\C{// int i; type is explicit}§
    11541153\end{cfa}
    11551154
     
    11591158H is implicit up-to exclusive range [0,H\R{)}.
    11601159\begin{cfa}
    1161 for ( ®5® )                                                             $\C{// for ( typeof(5) i; i < 5; i += 1 )}$
     1160for ( ®5® )                                                             §\C{// for ( typeof(5) i; i < 5; i += 1 )}§
    11621161\end{cfa}
    11631162\item
    11641163©~=© H is implicit up-to inclusive range [0,H\R{]}.
    11651164\begin{cfa}
    1166 for ( ®~=® 5 )                                                  $\C{// for ( typeof(5) i; i <= 5; i += 1 )}$
     1165for ( ®~=® 5 )                                                  §\C{// for ( typeof(5) i; i <= 5; i += 1 )}§
    11671166\end{cfa}
    11681167\item
    11691168L ©~©\index{~@©~©} H is explicit up-to exclusive range [L,H\R{)}.
    11701169\begin{cfa}
    1171 for ( 1 ®~® 5 )                                                 $\C{// for ( typeof(1) i = 1; i < 5; i += 1 )}$
     1170for ( 1 ®~® 5 )                                                 §\C{// for ( typeof(1) i = 1; i < 5; i += 1 )}§
    11721171\end{cfa}
    11731172\item
    11741173L ©~=©\index{~=@©~=©} H is explicit up-to inclusive range [L,H\R{]}.
    11751174\begin{cfa}
    1176 for ( 1 ®~=® 5 )                                                $\C{// for ( typeof(1) i = 1; i <= 5; i += 1 )}$
     1175for ( 1 ®~=® 5 )                                                §\C{// for ( typeof(1) i = 1; i <= 5; i += 1 )}§
    11771176\end{cfa}
    11781177\item
    11791178L ©-~©\index{-~@©-~©} H is explicit down-to exclusive range [H,L\R{)}, where L and H are implicitly interchanged to make the range down-to.
    11801179\begin{cfa}
    1181 for ( 1 ®-~® 5 )                                                $\C{// for ( typeof(1) i = 5; i > 0; i -= 1 )}$
     1180for ( 1 ®-~® 5 )                                                §\C{// for ( typeof(1) i = 5; i > 0; i -= 1 )}§
    11821181\end{cfa}
    11831182\item
    11841183L ©-~=©\index{-~=@©-~=©} H is explicit down-to inclusive range [H,L\R{]}, where L and H are implicitly interchanged to make the range down-to.
    11851184\begin{cfa}
    1186 for ( 1 ®-~=® 5 )                                               $\C{// for ( typeof(1) i = 5; i >= 0; i -= 1 )}$
     1185for ( 1 ®-~=® 5 )                                               §\C{// for ( typeof(1) i = 5; i >= 0; i -= 1 )}§
    11871186\end{cfa}
    11881187\item
    11891188©@© means put nothing in this field.
    11901189\begin{cfa}
    1191 for ( i; 1 ~ ®@® ~ 2 )                                  $\C{// for ( typeof(1) i = 1; \R{/* empty */}; i += 2 )}$
    1192 for ( i; 1 ~ 10 ~ ®@® )                                 $\C{// for ( typeof(1) i = 1; i < 10; \R{/* empty */} )}$
    1193 for ( i; 1 ~ ®@® ~ ®@® )                                $\C{// for ( typeof(1) i = 1; /*empty*/; \R{/* empty */} )}$
     1190for ( i; 1 ~ ®@® ~ 2 )                                  §\C{// for ( typeof(1) i = 1; \R{/* empty */}; i += 2 )}§
     1191for ( i; 1 ~ 10 ~ ®@® )                                 §\C{// for ( typeof(1) i = 1; i < 10; \R{/* empty */} )}§
     1192for ( i; 1 ~ ®@® ~ ®@® )                                §\C{// for ( typeof(1) i = 1; /*empty*/; \R{/* empty */} )}§
    11941193\end{cfa}
    11951194L cannot be elided for the up-to range, \lstinline{@ ~ 5}, and H for the down-to range, \lstinline{1 -~ @}, because then the loop index is uninitialized.
     
    11981197©:© means low another index.
    11991198\begin{cfa}
    1200 for ( i; 5 ®:® j; 2 ~ 12 ~ 3 )                  $\C{// for ( typeof(i) i = 1, j = 2; i < 5 \&\& j < 12; i += 1, j += 3 )}$
     1199for ( i; 5 ®:® j; 2 ~ 12 ~ 3 )                  §\C{// for ( typeof(i) i = 1, j = 2; i < 5 \&\& j < 12; i += 1, j += 3 )}§
    12011200\end{cfa}
    12021201\end{itemize}
    12031202\R{Warning}: specifying the down-to range maybe unexpected because the loop control \emph{implicitly} switches the L and H values (and toggles the increment/decrement for I):
    12041203\begin{cfa}
    1205 for ( i; 1 ~ 10 )                                               ${\C{// up range}$
    1206 for ( i; 1 -~ 10 )                                              ${\C{// down range}$
    1207 for ( i; ®10 -~ 1® )                                    ${\C{// \R{WRONG down range!}}}$
     1204for ( i; 1 ~ 10 )                                               §{\C{// up range}§
     1205for ( i; 1 -~ 10 )                                              §{\C{// down range}§
     1206for ( i; ®10 -~ 1® )                                    §{\C{// \R{WRONG down range!}}}§
    12081207\end{cfa}
    12091208The reason for this semantics is that the range direction can be toggled by adding/removing the minus, ©'-'©, versus interchanging the L and H expressions, which has a greater chance of introducing errors.
     
    13611360Grouping heterogeneous data into an \newterm{aggregate} (structure/union) is a common programming practice, and aggregates may be nested:
    13621361\begin{cfa}
    1363 struct Person {                                                 $\C{// aggregate}$
    1364         struct Name {                                           $\C{// nesting}$
     1362struct Person {                                                 §\C{// aggregate}§
     1363        struct Name {                                           §\C{// nesting}§
    13651364                char first[20], last[20];
    13661365        } name;
    1367         struct Address {                                        $\C{// nesting}$
     1366        struct Address {                                        §\C{// nesting}§
    13681367                ...
    13691368        } address;
     
    13741373\begin{cfa}
    13751374Person p
    1376 ®p.®name; ®p.®address; ®p.®sex;                 $\C{// access containing fields}$
     1375®p.®name; ®p.®address; ®p.®sex;                 §\C{// access containing fields}§
    13771376\end{cfa}
    13781377which extends to multiple levels of qualification for nested aggregates and multiple aggregates.
    13791378\begin{cfa}
    13801379struct Ticket { ... } t;
    1381 ®p.name®.first; ®p.address®.street;             $\C{// access nested fields}$
    1382 ®t.®departure; ®t.®cost;                                $\C{// access multiple aggregate}$
     1380®p.name®.first; ®p.address®.street;             §\C{// access nested fields}§
     1381®t.®departure; ®t.®cost;                                §\C{// access multiple aggregate}§
    13831382\end{cfa}
    13841383Repeated aggregate qualification is tedious and makes code difficult to read.
     
    13891388\begin{cfa}
    13901389struct S {
    1391         struct $\R{\LstCommentStyle{/* unnamed */}}$  { int g,  h; } __attribute__(( aligned(64) ));
     1390        struct §\R{\LstCommentStyle{/* unnamed */}}§  { int g,  h; } __attribute__(( aligned(64) ));
    13921391        int tag;
    1393         union $\R{\LstCommentStyle{/* unnamed */}}$  {
     1392        union §\R{\LstCommentStyle{/* unnamed */}}§  {
    13941393                struct { char c1,  c2; } __attribute__(( aligned(128) ));
    13951394                struct { int i1,  i2; };
     
    14051404struct S {
    14061405        char ®c®;   int ®i®;   double ®d®;
    1407         void f( /* S * this */ ) {                      $\C{// implicit ``this'' parameter}$
    1408                 ®c®;   ®i®;   ®d®;                              $\C{// this->c; this->i; this->d;}$
     1406        void f( /* S * this */ ) {                      §\C{// implicit ``this'' parameter}§
     1407                ®c®;   ®i®;   ®d®;                              §\C{// this->c; this->i; this->d;}§
    14091408        }
    14101409}
     
    14141413\begin{cfa}
    14151414struct T {
    1416         char ®m®;   int ®i®;   double ®n®;      $\C{// derived class variables}$
     1415        char ®m®;   int ®i®;   double ®n®;      §\C{// derived class variables}§
    14171416};
    14181417struct S : public T {
    1419         char ®c®;   int ®i®;   double ®d®;      $\C{// class variables}$
     1418        char ®c®;   int ®i®;   double ®d®;      §\C{// class variables}§
    14201419        void g( double ®d®, T & t ) {
    1421                 d;   ®t®.m;   ®t®.i;   ®t®.n;   $\C{// function parameter}$
    1422                 c;   i;   ®this->®d;   ®S::®d;  $\C{// class S variables}$
    1423                 m;   ®T::®i;   n;                               $\C{// class T variables}$
     1420                d;   ®t®.m;   ®t®.i;   ®t®.n;   §\C{// function parameter}§
     1421                c;   i;   ®this->®d;   ®S::®d;  §\C{// class S variables}§
     1422                m;   ®T::®i;   n;                               §\C{// class T variables}§
    14241423        }
    14251424};
     
    14311430Hence, the qualified fields become variables with the side-effect that it is simpler to write, easier to read, and optimize field references in a block.
    14321431\begin{cfa}
    1433 void f( S & this ) ®with ( this )® {    $\C{// with statement}$
    1434         ®c®;   ®i®;   ®d®;                                      $\C{// this.c, this.i, this.d}$
     1432void f( S & this ) ®with ( this )® {    §\C{// with statement}§
     1433        ®c®;   ®i®;   ®d®;                                      §\C{// this.c, this.i, this.d}§
    14351434}
    14361435\end{cfa}
    14371436with the generality of opening multiple aggregate-parameters:
    14381437\begin{cfa}
    1439 void g( S & s, T & t ) ®with ( s, t )® {$\C{// multiple aggregate parameters}$
    1440         c;   ®s.®i;   d;                                        $\C{// s.c, s.i, s.d}$
    1441         m;   ®t.®i;   n;                                        $\C{// t.m, t.i, t.n}$
     1438void g( S & s, T & t ) ®with ( s, t )® {§\C{// multiple aggregate parameters}§
     1439        c;   ®s.®i;   d;                                        §\C{// s.c, s.i, s.d}§
     1440        m;   ®t.®i;   n;                                        §\C{// t.m, t.i, t.n}§
    14421441}
    14431442\end{cfa}
     
    14621461struct R { int ®i®; int j; double ®m®; } r, w;
    14631462with ( r, q ) {
    1464         j + k;                                                          $\C{// unambiguous, r.j + q.k}$
    1465         m = 5.0;                                                        $\C{// unambiguous, q.m = 5.0}$
    1466         m = 1;                                                          $\C{// unambiguous, r.m = 1}$
    1467         int a = m;                                                      $\C{// unambiguous, a = r.i }$
    1468         double b = m;                                           $\C{// unambiguous, b = q.m}$
    1469         int c = r.i + q.i;                                      $\C{// disambiguate with qualification}$
    1470         (double)m;                                                      $\C{// disambiguate with cast}$
     1463        j + k;                                                          §\C{// unambiguous, r.j + q.k}§
     1464        m = 5.0;                                                        §\C{// unambiguous, q.m = 5.0}§
     1465        m = 1;                                                          §\C{// unambiguous, r.m = 1}§
     1466        int a = m;                                                      §\C{// unambiguous, a = r.i }§
     1467        double b = m;                                           §\C{// unambiguous, b = q.m}§
     1468        int c = r.i + q.i;                                      §\C{// disambiguate with qualification}§
     1469        (double)m;                                                      §\C{// disambiguate with cast}§
    14711470}
    14721471\end{cfa}
     
    14761475\begin{cfa}
    14771476with ( r ) {
    1478         i;                                                                      $\C{// unambiguous, r.i}$
     1477        i;                                                                      §\C{// unambiguous, r.i}§
    14791478        with ( q ) {
    1480                 i;                                                              $\C{// unambiguous, q.i}$
     1479                i;                                                              §\C{// unambiguous, q.i}§
    14811480        }
    14821481}
     
    14851484A cast can also be used to disambiguate among overload variables in a ©with© \emph{expression}:
    14861485\begin{cfa}
    1487 with ( w ) { ... }                                              $\C{// ambiguous, same name and no context}$
    1488 with ( (Q)w ) { ... }                                   $\C{// unambiguous, cast}$
     1486with ( w ) { ... }                                              §\C{// ambiguous, same name and no context}§
     1487with ( (Q)w ) { ... }                                   §\C{// unambiguous, cast}§
    14891488\end{cfa}
    14901489Because there is no left-side in the ©with© expression to implicitly disambiguate between the ©w© variables, it is necessary to explicitly disambiguate by casting ©w© to type ©Q© or ©R©.
     
    14931492\begin{cfa}
    14941493void f( S & s, char c ) with ( s ) {
    1495         ®s.c = c;®  i = 3;  d = 5.5;            $\C{// initialize fields}$
     1494        ®s.c = c;®  i = 3;  d = 5.5;            §\C{// initialize fields}§
    14961495}
    14971496\end{cfa}
     
    14991498To solve this problem, parameters \emph{not} explicitly opened are treated like an initialized aggregate:
    15001499\begin{cfa}
    1501 struct Params {                                                 $\C{// s explicitly opened so S \& s elided}$
     1500struct Params {                                                 §\C{// s explicitly opened so S \& s elided}§
    15021501        char c;
    15031502} params;
     
    15051504and implicitly opened \emph{after} a function-body open, to give them higher priority:
    15061505\begin{cfa}
    1507 void f( S & s, char ®c® ) with ( s ) ®with( $\emph{\R{params}}$ )® { // syntax not allowed, illustration only
     1506void f( S & s, char ®c® ) with ( s ) ®with( §\emph{\R{params}}§ )® { // syntax not allowed, illustration only
    15081507        s.c = ®c;®  i = 3;  d = 5.5;
    15091508}
     
    15381537
    15391538\begin{cfa}
    1540 exception_t E {}; $\C{// exception type}$
     1539exception_t E {}; §\C{// exception type}§
    15411540void f(...) {
    1542         ... throw E{}; ... $\C{// termination}$
    1543         ... throwResume E{}; ... $\C{// resumption}$
     1541        ... throw E{}; ... §\C{// termination}§
     1542        ... throwResume E{}; ... §\C{// resumption}§
    15441543}
    15451544try {
    15461545        f(...);
    1547 } catch( E e ; $boolean-predicate$ ) {  $\C{// termination handler}$
     1546} catch( E e ; §boolean-predicate§ ) {  §\C{// termination handler}§
    15481547        // recover and continue
    1549 } catchResume( E e ; $boolean-predicate$ ) { $\C{// resumption handler}$
     1548} catchResume( E e ; §boolean-predicate§ ) { §\C{// resumption handler}§
    15501549        // repair and return
    15511550} finally {
     
    16241623For example, a routine returning a \Index{pointer} to an array of integers is defined and used in the following way:
    16251624\begin{cfa}
    1626 int ®(*®f®())[®5®]® {...}; $\C{// definition}$
    1627  ... ®(*®f®())[®3®]® += 1; $\C{// usage}$
     1625int ®(*®f®())[®5®]® {...}; §\C{// definition}§
     1626 ... ®(*®f®())[®3®]® += 1; §\C{// usage}§
    16281627\end{cfa}
    16291628Essentially, the return type is wrapped around the routine name in successive layers (like an \Index{onion}).
     
    18701869For example, \Index*{Algol68}~\cite{Algol68} infers pointer dereferencing to select the best meaning for each pointer usage
    18711870\begin{cfa}
    1872 p2 = p1 + x; $\C{// compiler infers *p2 = *p1 + x;}$
     1871p2 = p1 + x; §\C{// compiler infers *p2 = *p1 + x;}§
    18731872\end{cfa}
    18741873Algol68 infers the following dereferencing ©*p2 = *p1 + x©, because adding the arbitrary integer value in ©x© to the address of ©p1© and storing the resulting address into ©p2© is an unlikely operation.
     
    18781877In C, objects of pointer type always manipulate the pointer object's address:
    18791878\begin{cfa}
    1880 p1 = p2; $\C{// p1 = p2\ \ rather than\ \ *p1 = *p2}$
    1881 p2 = p1 + x; $\C{// p2 = p1 + x\ \ rather than\ \ *p2 = *p1 + x}$
     1879p1 = p2; §\C{// p1 = p2\ \ rather than\ \ *p1 = *p2}§
     1880p2 = p1 + x; §\C{// p2 = p1 + x\ \ rather than\ \ *p2 = *p1 + x}§
    18821881\end{cfa}
    18831882even though the assignment to ©p2© is likely incorrect, and the programmer probably meant:
    18841883\begin{cfa}
    1885 p1 = p2; $\C{// pointer address assignment}$
    1886 ®*®p2 = ®*®p1 + x; $\C{// pointed-to value assignment / operation}$
     1884p1 = p2; §\C{// pointer address assignment}§
     1885®*®p2 = ®*®p1 + x; §\C{// pointed-to value assignment / operation}§
    18871886\end{cfa}
    18881887The C semantics work well for situations where manipulation of addresses is the primary meaning and data is rarely accessed, such as storage management (©malloc©/©free©).
     
    19011900\begin{cfa}
    19021901int x, y, ®&® r1, ®&® r2, ®&&® r3;
    1903 ®&®r1 = &x; $\C{// r1 points to x}$
    1904 ®&®r2 = &r1; $\C{// r2 points to x}$
    1905 ®&®r1 = &y; $\C{// r1 points to y}$
    1906 ®&&®r3 = ®&®&r2; $\C{// r3 points to r2}$
    1907 r2 = ((r1 + r2) * (r3 - r1)) / (r3 - 15); $\C{// implicit dereferencing}$
     1902®&®r1 = &x; §\C{// r1 points to x}§
     1903®&®r2 = &r1; §\C{// r2 points to x}§
     1904®&®r1 = &y; §\C{// r1 points to y}§
     1905®&&®r3 = ®&®&r2; §\C{// r3 points to r2}§
     1906r2 = ((r1 + r2) * (r3 - r1)) / (r3 - 15); §\C{// implicit dereferencing}§
    19081907\end{cfa}
    19091908Except for auto-dereferencing by the compiler, this reference example is the same as the previous pointer example.
     
    19201919For a \CFA reference type, the cancellation on the left-hand side of assignment leaves the reference as an address (\Index{lvalue}):
    19211920\begin{cfa}
    1922 (&®*®)r1 = &x; $\C{// (\&*) cancel giving address in r1 not variable pointed-to by r1}$
     1921(&®*®)r1 = &x; §\C{// (\&*) cancel giving address in r1 not variable pointed-to by r1}§
    19231922\end{cfa}
    19241923Similarly, the address of a reference can be obtained for assignment or computation (\Index{rvalue}):
    19251924\begin{cfa}
    1926 (&(&®*®)®*®)r3 = &(&®*®)r2; $\C{// (\&*) cancel giving address in r2, (\&(\&*)*) cancel giving address in r3}$
     1925(&(&®*®)®*®)r3 = &(&®*®)r2; §\C{// (\&*) cancel giving address in r2, (\&(\&*)*) cancel giving address in r3}§
    19271926\end{cfa}
    19281927Cancellation\index{cancellation!pointer/reference}\index{pointer!cancellation} works to arbitrary depth.
     
    19321931int x, *p1 = &x, **p2 = &p1, ***p3 = &p2,
    19331932                 &r1 = x,    &&r2 = r1,   &&&r3 = r2;
    1934 ***p3 = 3; $\C{// change x}$
    1935 r3 = 3; $\C{// change x, ***r3}$
    1936 **p3 = ...; $\C{// change p1}$
    1937 &r3 = ...; $\C{// change r1, (\&*)**r3, 1 cancellation}$
    1938 *p3 = ...; $\C{// change p2}$
    1939 &&r3 = ...; $\C{// change r2, (\&(\&*)*)*r3, 2 cancellations}$
    1940 &&&r3 = p3; $\C{// change r3 to p3, (\&(\&(\&*)*)*)r3, 3 cancellations}$
     1933***p3 = 3; §\C{// change x}§
     1934r3 = 3; §\C{// change x, ***r3}§
     1935**p3 = ...; §\C{// change p1}§
     1936&r3 = ...; §\C{// change r1, (\&*)**r3, 1 cancellation}§
     1937*p3 = ...; §\C{// change p2}§
     1938&&r3 = ...; §\C{// change r2, (\&(\&*)*)*r3, 2 cancellations}§
     1939&&&r3 = p3; §\C{// change r3 to p3, (\&(\&(\&*)*)*)r3, 3 cancellations}§
    19411940\end{cfa}
    19421941Furthermore, both types are equally performant, as the same amount of dereferencing occurs for both types.
     
    19451944As for a pointer type, a reference type may have qualifiers:
    19461945\begin{cfa}
    1947 const int cx = 5; $\C{// cannot change cx;}$
    1948 const int & cr = cx; $\C{// cannot change what cr points to}$
    1949 ®&®cr = &cx; $\C{// can change cr}$
    1950 cr = 7; $\C{// error, cannot change cx}$
    1951 int & const rc = x; $\C{// must be initialized}$
    1952 ®&®rc = &x; $\C{// error, cannot change rc}$
    1953 const int & const crc = cx; $\C{// must be initialized}$
    1954 crc = 7; $\C{// error, cannot change cx}$
    1955 ®&®crc = &cx; $\C{// error, cannot change crc}$
     1946const int cx = 5; §\C{// cannot change cx;}§
     1947const int & cr = cx; §\C{// cannot change what cr points to}§
     1948®&®cr = &cx; §\C{// can change cr}§
     1949cr = 7; §\C{// error, cannot change cx}§
     1950int & const rc = x; §\C{// must be initialized}§
     1951®&®rc = &x; §\C{// error, cannot change rc}§
     1952const int & const crc = cx; §\C{// must be initialized}§
     1953crc = 7; §\C{// error, cannot change cx}§
     1954®&®crc = &cx; §\C{// error, cannot change crc}§
    19561955\end{cfa}
    19571956Hence, for type ©& const©, there is no pointer assignment, so ©&rc = &x© is disallowed, and \emph{the address value cannot be the null pointer unless an arbitrary pointer is coerced\index{coercion} into the reference}:
    19581957\begin{cfa}
    1959 int & const cr = *0; $\C{// where 0 is the int * zero}$
     1958int & const cr = *0; §\C{// where 0 is the int * zero}§
    19601959\end{cfa}
    19611960Note, constant reference-types do not prevent \Index{addressing errors} because of explicit storage-management:
     
    19641963cr = 5;
    19651964free( &cr );
    1966 cr = 7; $\C{// unsound pointer dereference}$
     1965cr = 7; §\C{// unsound pointer dereference}§
    19671966\end{cfa}
    19681967
     
    19721971\begin{cquote}
    19731972\begin{tabular}{@{}l@{\hspace{3em}}l@{}}
    1974 \multicolumn{1}{c@{\hspace{3em}}}{\textbf{Cy}}  & \multicolumn{1}{c}{\textbf{\CFA}}     \\
     1973\multicolumn{1}{c@{\hspace{3em}}}{\textbf{C}}   & \multicolumn{1}{c}{\textbf{\CFA}}     \\
    19751974\begin{cfa}
    19761975const int * ®const® * ®const® ccp;
     
    19881987Finally, like pointers, references are usable and composable with other type operators and generators.
    19891988\begin{cfa}
    1990 int w, x, y, z, & ar[3] = { x, y, z }; $\C{// initialize array of references}$
    1991 &ar[1] = &w; $\C{// change reference array element}$
    1992 typeof( ar[1] ) p; $\C{// (gcc) is int, \ie the type of referenced object}$
    1993 typeof( &ar[1] ) q; $\C{// (gcc) is int \&, \ie the type of reference}$
    1994 sizeof( ar[1] ) == sizeof( int ); $\C{// is true, \ie the size of referenced object}$
    1995 sizeof( &ar[1] ) == sizeof( int *) $\C{// is true, \ie the size of a reference}$
     1989int w, x, y, z, & ar[3] = { x, y, z }; §\C{// initialize array of references}§
     1990&ar[1] = &w; §\C{// change reference array element}§
     1991typeof( ar[1] ) p; §\C{// (gcc) is int, \ie the type of referenced object}§
     1992typeof( &ar[1] ) q; §\C{// (gcc) is int \&, \ie the type of reference}§
     1993sizeof( ar[1] ) == sizeof( int ); §\C{// is true, \ie the size of referenced object}§
     1994sizeof( &ar[1] ) == sizeof( int *) §\C{// is true, \ie the size of a reference}§
    19961995\end{cfa}
    19971996
     
    20102009Therefore, for pointer/reference initialization, the initializing value must be an address not a value.
    20112010\begin{cfa}
    2012 int * p = &x; $\C{// assign address of x}$
    2013 ®int * p = x;® $\C{// assign value of x}$
    2014 int & r = x; $\C{// must have address of x}$
     2011int * p = &x;                           §\C{// assign address of x}§
     2012®int * p = x;®                          §\C{// assign value of x}§
     2013int & r = x;                            §\C{// must have address of x}§
    20152014\end{cfa}
    20162015Like the previous example with C pointer-arithmetic, it is unlikely assigning the value of ©x© into a pointer is meaningful (again, a warning is usually given).
     
    20212020Similarly, when a reference type is used for a parameter/return type, the call-site argument does not require a reference operator for the same reason.
    20222021\begin{cfa}
    2023 int & f( int & r ); $\C{// reference parameter and return}$
    2024 z = f( x ) + f( y ); $\C{// reference operator added, temporaries needed for call results}$
     2022int & f( int & r );                     §\C{// reference parameter and return}§
     2023z = f( x ) + f( y );            §\C{// reference operator added, temporaries needed for call results}§
    20252024\end{cfa}
    20262025Within routine ©f©, it is possible to change the argument by changing the corresponding parameter, and parameter ©r© can be locally reassigned within ©f©.
     
    20492048void f( int & r );
    20502049void g( int * p );
    2051 f( 3 );                   g( ®&®3 ); $\C{// compiler implicit generates temporaries}$
    2052 f( x + y );             g( ®&®(x + y) ); $\C{// compiler implicit generates temporaries}$
     2050f( 3 );                   g( ®&®3 ); §\C{// compiler implicit generates temporaries}§
     2051f( x + y );             g( ®&®(x + y) ); §\C{// compiler implicit generates temporaries}§
    20532052\end{cfa}
    20542053Essentially, there is an implicit \Index{rvalue} to \Index{lvalue} conversion in this case.\footnote{
     
    20612060\begin{cfa}
    20622061void f( int i );
    2063 void (* fp)( int ); $\C{// routine pointer}$
    2064 fp = f; $\C{// reference initialization}$
    2065 fp = &f; $\C{// pointer initialization}$
    2066 fp = *f; $\C{// reference initialization}$
    2067 fp(3); $\C{// reference invocation}$
    2068 (*fp)(3); $\C{// pointer invocation}$
     2062void (* fp)( int );                     §\C{// routine pointer}§
     2063fp = f;                                         §\C{// reference initialization}§
     2064fp = &f;                                        §\C{// pointer initialization}§
     2065fp = *f;                                        §\C{// reference initialization}§
     2066fp(3);                                          §\C{// reference invocation}§
     2067(*fp)(3);                                       §\C{// pointer invocation}§
    20692068\end{cfa}
    20702069While C's treatment of routine objects has similarity to inferring a reference type in initialization contexts, the examples are assignment not initialization, and all possible forms of assignment are possible (©f©, ©&f©, ©*f©) without regard for type.
    20712070Instead, a routine object should be referenced by a ©const© reference:
    20722071\begin{cfa}
    2073 ®const® void (®&® fr)( int ) = f; $\C{// routine reference}$
    2074 fr = ... $\C{// error, cannot change code}$
    2075 &fr = ...; $\C{// changing routine reference}$
    2076 fr( 3 ); $\C{// reference call to f}$
    2077 (*fr)(3); $\C{// error, incorrect type}$
     2072®const® void (®&® fr)( int ) = f; §\C{// routine reference}§
     2073fr = ...;                                       §\C{// error, cannot change code}§
     2074&fr = ...;                                      §\C{// changing routine reference}§
     2075fr( 3 );                                        §\C{// reference call to f}§
     2076(*fr)(3);                                       §\C{// error, incorrect type}§
    20782077\end{cfa}
    20792078because the value of the routine object is a routine literal, \ie the routine code is normally immutable during execution.\footnote{
     
    20972096int x, * px, ** ppx, *** pppx, **** ppppx;
    20982097int & rx = x, && rrx = rx, &&& rrrx = rrx ;
    2099 x = rrrx; $\C[2.0in]{// rrrx is an lvalue with type int \&\&\& (equivalent to x)}$
    2100 px = &rrrx; $\C{// starting from rrrx, \&rrrx is an rvalue with type int *\&\&\& (\&x)}$
    2101 ppx = &&rrrx; $\C{// starting from \&rrrx, \&\&rrrx is an rvalue with type int **\&\& (\&rx)}$
    2102 pppx = &&&rrrx; $\C{// starting from \&\&rrrx, \&\&\&rrrx is an rvalue with type int ***\& (\&rrx)}$
    2103 ppppx = &&&&rrrx; $\C{// starting from \&\&\&rrrx, \&\&\&\&rrrx is an rvalue with type int **** (\&rrrx)}$
     2098x = rrrx;                                       §\C{// rrrx is an lvalue with type int \&\&\& (equivalent to x)}§
     2099px = &rrrx;                                     §\C{// starting from rrrx, \&rrrx is an rvalue with type int *\&\&\& (\&x)}§
     2100ppx = &&rrrx;                           §\C{// starting from \&rrrx, \&\&rrrx is an rvalue with type int **\&\& (\&rx)}§
     2101pppx = &&&rrrx;                         §\C{// starting from \&\&rrrx, \&\&\&rrrx is an rvalue with type int ***\& (\&rrx)}§
     2102ppppx = &&&&rrrx;                       §\C{// starting from \&\&\&rrrx, \&\&\&\&rrrx is an rvalue with type int **** (\&rrrx)}§
    21042103\end{cfa}
    21052104The following example shows the second rule applied to different \Index{lvalue} contexts:
     
    21072106int x, * px, ** ppx, *** pppx;
    21082107int & rx = x, && rrx = rx, &&& rrrx = rrx ;
    2109 rrrx = 2; $\C{// rrrx is an lvalue with type int \&\&\& (equivalent to x)}$
    2110 &rrrx = px; $\C{// starting from rrrx, \&rrrx is an rvalue with type int *\&\&\& (rx)}$
    2111 &&rrrx = ppx; $\C{// starting from \&rrrx, \&\&rrrx is an rvalue with type int **\&\& (rrx)}$
    2112 &&&rrrx = pppx; $\C{// starting from \&\&rrrx, \&\&\&rrrx is an rvalue with type int ***\& (rrrx)}\CRT$
     2108rrrx = 2;                                       §\C{// rrrx is an lvalue with type int \&\&\& (equivalent to x)}§
     2109&rrrx = px;                                     §\C{// starting from rrrx, \&rrrx is an rvalue with type int *\&\&\& (rx)}§
     2110&&rrrx = ppx;                           §\C{// starting from \&rrrx, \&\&rrrx is an rvalue with type int **\&\& (rrx)}§
     2111&&&rrrx = pppx;                         §\C{// starting from \&\&rrrx, \&\&\&rrrx is an rvalue with type int ***\& (rrrx)}§
    21132112\end{cfa}
    21142113
     
    21232122\begin{cfa}
    21242123int x;
    2125 x + 1; $\C[2.0in]{// lvalue variable (int) converts to rvalue for expression}$
     2124x + 1;                                          §\C{// lvalue variable (int) converts to rvalue for expression}§
    21262125\end{cfa}
    21272126An rvalue has no type qualifiers (©cv©), so the lvalue qualifiers are dropped.
     
    21332132\begin{cfa}
    21342133int x, &r = x, f( int p );
    2135 x = ®r® + f( ®r® ); $\C{// lvalue reference converts to rvalue}$
     2134x = ®r® + f( ®r® );                     §\C{// lvalue reference converts to rvalue}§
    21362135\end{cfa}
    21372136An rvalue has no type qualifiers (©cv©), so the reference qualifiers are dropped.
     
    21402139lvalue to reference conversion: \lstinline[deletekeywords=lvalue]{lvalue-type cv1 T} converts to ©cv2 T &©, which allows implicitly converting variables to references.
    21412140\begin{cfa}
    2142 int x, &r = ®x®, f( int & p ); $\C{// lvalue variable (int) convert to reference (int \&)}$
    2143 f( ®x® ); $\C{// lvalue variable (int) convert to reference (int \&)}\CRT$
     2141int x, &r = ®x®, f( int & p ); §\C{// lvalue variable (int) convert to reference (int \&)}§
     2142f( ®x® );                                       §\C{// lvalue variable (int) convert to reference (int \&)}§
    21442143\end{cfa}
    21452144Conversion can restrict a type, where ©cv1© $\le$ ©cv2©, \eg passing an ©int© to a ©const volatile int &©, which has low cost.
     
    21512150\begin{cfa}
    21522151int x, & f( int & p );
    2153 f( ®x + 3® );   $\C[1.5in]{// rvalue parameter (int) implicitly converts to lvalue temporary reference (int \&)}$
    2154 ®&f®(...) = &x; $\C{// rvalue result (int \&) implicitly converts to lvalue temporary reference (int \&)}\CRT$
     2152f( ®x + 3® );                           §\C{// rvalue parameter (int) implicitly converts to lvalue temporary reference (int \&)}§
     2153®&f®(...) = &x;                         §\C{// rvalue result (int \&) implicitly converts to lvalue temporary reference (int \&)}§
    21552154\end{cfa}
    21562155In both case, modifications to the temporary are inaccessible (\Index{warning}).
     
    23192318
    23202319
     2320\section{\lstinline{string} Type}
     2321\label{s:stringType}
     2322
     2323The \CFA \Indexc{string} type is for manipulation of dynamically-size character-strings versus C \Indexc{char *} type for manipulation of statically-size null-terminated character-strings.
     2324That is, the amount of storage for a \CFA string changes dynamically at runtime to fit the string size, whereas the amount of storage for a C string is fixed at compile time.
     2325Hence, a ©string© declaration does not specify a maximum length;
     2326as a string dynamically grows and shrinks in size, so does its underlying storage.
     2327In contrast, a C string also dynamically grows and shrinks is size, but its underlying storage is fixed.
     2328The maximum storage for a \CFA ©string© value is ©size_t© characters, which is $2^{32}$ or $2^{64}$ respectively.
     2329A \CFA string manages its length separately from the string, so there is no null (©'\0'©) terminating value at the end of a string value.
     2330Hence, a \CFA string cannot be passed to a C string manipulation routine, such as ©strcat©.
     2331Like C strings, the characters in a ©string© are numbered starting from 0.
     2332
     2333The following operations have been defined to manipulate an instance of type ©string©.
     2334The discussion assumes the following declarations and assignment statements are executed.
     2335\begin{cfa}
     2336#include ®<string.hfa>®
     2337®string® s, peter, digit, alpha, punctuation, ifstmt;
     2338int i;
     2339peter  = "PETER";
     2340digit  = "0123456789";
     2341punctuation = "().,";
     2342ifstmt = "IF (A > B) {";
     2343\end{cfa}
     2344Note, the include file \Indexc{string.hfa} to access type ©string©.
     2345
     2346
     2347\subsection{Implicit String Conversions}
     2348
     2349The types ©char©, ©char *©, ©int©, ©double©, ©_Complex©, including signness and different sizes, implicitly convert to type ©string©.
     2350\VRef[Figure]{f:ImplicitStringConversions} shows examples of implicit conversion between C strings, integral, floating-point and complex types to ©string©
     2351The implicit conversions can be specified explicitly, as in:
     2352\begin{cfa}
     2353s = string( "abc" );            // converts char * to string
     2354s = string( 5 );                        // converts int to string
     2355s = string( 5.5 );                      // converts double to string
     2356\end{cfa}
     2357Conversions from ©string© to ©char *© are supported but with restrictions.
     2358Explicit As well, when a string is converted to a ©char *©, the storage for the ©char *© is created by the conversion operation, which must be subsequently deleted:
     2359\begin{cfa}
     2360string x = "abc";
     2361char *p = x;                            // convert from string to char *
     2362...
     2363delete p;                                       // free storage created for p
     2364\end{cfa}
     2365
     2366\begin{figure}
     2367\begin{tabular}{@{}l@{\hspace{15pt}}|@{\hspace{15pt}}l@{}}
     2368\begin{cfa}
     2369//      string s = 5;                                   sout | s;
     2370        string s;
     2371        // conversion of char and char * to string
     2372        s = 'x';                                                §\CD{sout | s;}§
     2373        s = "abc";                                              §\CD{sout | s;}§
     2374        char cs[5] = "abc";
     2375        s = cs;                                                 §\CD{sout | s;}§
     2376        // conversion of integral, floating-point, and complex to string
     2377        s = 45hh;                                               §\CD{sout | s;}§
     2378        s = 45h;                                                §\CD{sout | s;}§
     2379        s = -(ssize_t)MAX - 1;                  §\CD{sout | s;}§
     2380        s = (size_t)MAX;                                §\CD{sout | s;}§
     2381        s = 5.5;                                                §\CD{sout | s;}§
     2382        s = 5.5L;                                               §\CD{sout | s;}§
     2383        s = 5.5+3.4i;                                   §\CD{sout | s;}§
     2384        s = 5.5L+3.4Li;                                 §\CD{sout | s;}§
     2385        // safe conversion from string to char *
     2386        strncpy( cs, s, sizeof(cs) );   §\CD{sout | cs;}§
     2387        char * cp = s;                                  §\CD{sout | cp; // ownership}§
     2388        delete( cp );
     2389        cp = s + ' ' + s;                               §\CD{sout | cp; // ownership}§
     2390        delete( cp );
     2391\end{cfa}
     2392&
     2393\begin{cfa}
     2394
     2395
     2396
     2397x
     2398abc
     2399
     2400abc
     2401
     240245
     240345
     2404-9223372036854775808
     240518446744073709551615
     24065.5
     24075.5
     24085.5+3.4i
     24095.5+3.4i
     2410
     24115.5+
     24125.5+3.4i
     2413
     24145.5+3.4i 5.5+3.4i
     2415
     2416\end{cfa}
     2417\end{tabular}
     2418\caption{Implicit String Conversions}
     2419\label{f:ImplicitStringConversions}
     2420\end{figure}
     2421
     2422
     2423\subsection{Comparison Operators}
     2424
     2425The binary relational and equality operators ©<©, ©<=©, ©>©, ©>=©, ©==©, ©!=© compare ©string© using lexicographical ordering, where longer strings are greater than shorter strings.
     2426
     2427
     2428\subsection{Concatenation}
     2429
     2430The binary operator ©+© concatenates two strings.
     2431\begin{cfa}
     2432s = peter + digit;                                      §\C{// s is assigned "PETER0123456789"}§
     2433s += peter;                                                     §\C{// s is assigned "PETER0123456789PETER"}§
     2434\end{cfa}
     2435There is also an assignment form ©+=©.
     2436
     2437
     2438\subsection{Repetition}
     2439
     2440The binary operator \Indexc{*} returns a string that is the string repeated ©n© times.
     2441If ©n = 0©, a zero length string, ©""© is returned.
     2442\begin{cfa}
     2443s = (peter + ' ') * 3;                          §\C{// s is assigned "PETER PETER PETER"}§
     2444\end{cfa}
     2445There is also an assignment form ©*=©.
     2446
     2447
     2448\subsection{Length}
     2449
     2450The ©length© operation
     2451\begin{cfa}
     2452int length()
     2453\end{cfa}
     2454returns the length of a string variable.
     2455\begin{cfa}
     2456i = peter.length();                     §\C{// i is assigned the value 5}§
     2457\end{cfa}
     2458
     2459
     2460\subsection{Substring}
     2461The substring operation:
     2462\begin{cfa}
     2463string operator () (int start, int lnth);
     2464\end{cfa}
     2465performs a substring operation that returns the string starting at a specified position (©start©) in the current string, and having the specified length (©lnth©).
     2466A negative starting position is a specification from the right end of the string.
     2467A negative length means that characters are selected in the opposite (right to left) direction from the starting position.
     2468If the substring request extends beyond the beginning or end of the string, it is clipped (shortened) to the bounds of the string.
     2469If the substring request is completely outside of the original string, a null string located at the end of the original string is returned.
     2470\begin{cfa}
     2471s = peter( 2, 3 );                      §\C{// s is assigned "ETE"}§
     2472s = peter( 4, -3 );                     §\C{// s is assigned "ETE", length is opposite direction}§
     2473s = peter( 2, 8 );                      §\C{// s is assigned "ETER", length is clipped to 4}§
     2474s = peter( 0, -1 );                     §\C{// s is assigned "", beyond string so clipped to null}§
     2475s = peter(-1, -1 );                     §\C{// s is assigned "R", start and length are negative}§
     2476\end{cfa}
     2477The substring operation can also appear on the left hand side of the assignment operator.
     2478The substring is replaced by the value on the right hand side of the assignment.
     2479The length of the right-hand-side value may be shorter, the same length, or longer than the length of the substring that is selected on the left hand side of the assignment.
     2480\begin{cfa}[mathescape=false]
     2481digit( 3, 3 ) = "";             §\C{// digit is assigned "0156789"}§
     2482digit( 4, 3 ) = "xyz";          §\C{// digit is assigned "015xyz9"}§
     2483digit( 7, 0 ) = "***";          §\C{// digit is assigned "015xyz***9"}§
     2484digit(-4, 3 ) = "$$$";          §\C{// digit is assigned "015xyz\$\$\$9"}§
     2485\end{cfa}
     2486A substring is treated as a pointer into the base (substringed) string rather than creating a copy of the subtext.
     2487As with all pointers, if the item they are pointing at is changed, then the pointer is referring to the changed item.
     2488Pointers to the result value of a substring operation are defined to always start at the same location in their base string as long as that starting location exists, independent of changes to themselves or the base string.
     2489However, if the base string value changes, this may affect the values of one or more of the substrings to that base string.
     2490If the base string value shortens so that its end is before the starting location of a substring, resulting in the substring starting location disappearing, the substring becomes a null string located at the end of the base string.
     2491
     2492The following example illustrates passing the results of substring operations by reference and by value to a subprogram.
     2493Notice the side-effects to other reference parameters as one is modified.
     2494\begin{cfa}
     2495main() {
     2496        string x = "xxxxxxxxxxxxx";
     2497        test( x, x(1,3), x(3,3), x(5,5), x(9,5), x(9,5) );
     2498}
     2499
     2500// x, a, b, c, & d are substring results passed by reference
     2501// e is a substring result passed by value
     2502void test(string &x, string &a, string &b, string &c, string &d, string e) {
     2503                                                        §\C{//   x                                a               b               c               d               e}§
     2504        a( 1, 2 ) = "aaa";              §\C{// aaaxxxxxxxxxxx   aaax    axx             xxxxx   xxxxx   xxxxx}§
     2505        b( 2, 12 ) = "bbb";             §\C{// aaabbbxxxxxxxxx  aaab    abbb    bbxxx   xxxxx   xxxxx}§
     2506        c( 4, 5 ) = "ccc";              §\C{// aaabbbxcccxxxxxx aaab    abbb    bbxccc  ccxxx   xxxxx}§
     2507        c = "yyy";                              §\C{// aaabyyyxxxxxx    aaab    abyy    yyy             xxxxx   xxxxx}§
     2508        d( 1, 3 ) = "ddd";              §\C{// aaabyyyxdddxx    aaab    abyy    yyy             dddxx   xxxxx}§
     2509        e( 1, 3 ) = "eee";              §\C{// aaabyyyxdddxx    aaab    abyy    yyy             dddxx   eeexx}§
     2510        x = e;                                  §\C{// eeexx                    eeex    exx             x                               eeexx}§
     2511}
     2512\end{cfa}
     2513
     2514There is an assignment form of substring in which only the starting position is specified and the length is assumed to be the remainder of the string.
     2515\begin{cfa}
     2516string operator () (int start);
     2517\end{cfa}
     2518For example:
     2519\begin{cfa}
     2520s = peter( 2 );                         §\C{// s is assigned "ETER"}§
     2521peter( 2 ) = "IPER";            §\C{// peter is assigned "PIPER"}§
     2522\end{cfa}
     2523It is also possible to substring using a string as the index for selecting the substring portion of the string.
     2524\begin{cfa}
     2525string operator () (const string &index);
     2526\end{cfa}
     2527For example:
     2528\begin{cfa}[mathescape=false]
     2529digit( "xyz$$$" ) = "678";      §\C{// digit is assigned "0156789"}§
     2530digit( "234") = "***";          §\C{// digit is assigned "0156789***"}§
     2531\end{cfa}
     2532%$
     2533
     2534
     2535\subsection{Searching}
     2536
     2537The ©index© operation
     2538\begin{cfa}
     2539int index( const string &key, int start = 1, occurrence occ = first );
     2540\end{cfa}
     2541returns the position of the first or last occurrence of the ©key© (depending on the occurrence indicator ©occ© that is either ©first© or ©last©) in the current string starting the search at position ©start©.
     2542If the ©key© does not appear in the current string, the length of the current string plus one is returned.
     2543%If the ©key© has zero length, the value 1 is returned regardless of what the current string contains.
     2544A negative starting position is a specification from the right end of the string.
     2545\begin{cfa}
     2546i = digit.index( "567" );                       §\C{// i is assigned 3}§
     2547i = digit.index( "567", 7 );            §\C{// i is assigned 11}§
     2548i = digit.index( "567", -1, last );     §\C{// i is assigned 3}§
     2549i = peter.index( "E", 5, last );        §\C{// i is assigned 4}§
     2550\end{cfa}
     2551
     2552The next two string operations test a string to see if it is or is not composed completely of a particular class of characters.
     2553For example, are the characters of a string all alphabetic or all numeric?
     2554Use of these operations involves a two step operation.
     2555First, it is necessary to create an instance of type ©strmask© and initialize it to a string containing the characters of the particular character class, as in:
     2556\begin{cfa}
     2557strmask digitmask = digit;
     2558strmask alphamask = string( "abcdefghijklmnopqrstuvwxyz" );
     2559\end{cfa}
     2560Second, the character mask is used in the functions ©include© and ©exclude© to check a string for compliance of its characters with the characters indicated by the mask.
     2561
     2562The ©include© operation
     2563\begin{cfa}
     2564int include( const strmask &, int = 1, occurrence occ = first );
     2565\end{cfa}
     2566returns the position of the first or last character (depending on the occurrence indicator, which is either ©first© or ©last©) in the current string that does not appear in the ©mask© starting the search at position ©start©;
     2567hence it skips over characters in the current string that are included (in) the ©mask©.
     2568The characters in the current string do not have to be in the same order as the ©mask©.
     2569If all the characters in the current string appear in the ©mask©, the length of the current string plus one is returned, regardless of which occurrence is being searched for.
     2570A negative starting position is a specification from the right end of the string.
     2571\begin{cfa}
     2572i = peter.include( digitmask ); §\C{// i is assigned 1}§
     2573i = peter.include( alphamask ); §\C{// i is assigned 6}§
     2574\end{cfa}
     2575
     2576The ©exclude© operation
     2577\begin{cfa}
     2578int exclude( string &mask, int start = 1, occurrence occ = first )
     2579\end{cfa}
     2580returns the position of the first or last character (depending on the occurrence indicator, which is either ©first© or ©last©) in the current string that does appear in the ©mask© string starting the search at position ©start©;
     2581hence it skips over characters in the current string that are excluded from (not in) in the ©mask© string.
     2582The characters in the current string do not have to be in the same order as the ©mask© string.
     2583If all the characters in the current string do NOT appear in the ©mask© string, the length of the current string plus one is returned, regardless of which occurrence is being searched for.
     2584A negative starting position is a specification from the right end of the string.
     2585\begin{cfa}
     2586i = peter.exclude( digitmask );         §\C{// i is assigned 6}§
     2587i = ifstmt.exclude( strmask( punctuation ) ); §\C{// i is assigned 4}§
     2588\end{cfa}
     2589
     2590The ©includeStr© operation:
     2591\begin{cfa}
     2592string includeStr( strmask &mask, int start = 1, occurrence occ = first )
     2593\end{cfa}
     2594returns the longest substring of leading or trailing characters (depending on the occurrence indicator, which is either ©first© or ©last©) of the current string that ARE included in the ©mask© string starting the search at position ©start©.
     2595A negative starting position is a specification from the right end of the string.
     2596\begin{cfa}
     2597s = peter.includeStr( alphamask );      §\C{// s is assigned "PETER"}§
     2598s = ifstmt.includeStr( alphamask );     §\C{// s is assigned "IF"}§
     2599s = peter.includeStr( digitmask );      §\C{// s is assigned ""}§
     2600\end{cfa}
     2601
     2602The ©excludeStr© operation:
     2603\begin{cfa}
     2604string excludeStr( strmask &mask, int start = 1, occurrence = first )
     2605\end{cfa}
     2606returns the longest substring of leading or trailing characters (depending on the occurrence indicator, which is either ©first© or ©last©) of the current string that are excluded (NOT) in the ©mask© string starting the search at position ©start©.
     2607A negative starting position is a specification from the right end of the string.
     2608\begin{cfa}
     2609s = peter.excludeStr( digitmask);       §\C{// s is assigned "PETER"}§
     2610s = ifstmt.excludeStr( strmask( punctuation ) ); §\C{// s is assigned "IF "}§
     2611s = peter.excludeStr( alphamask);       §\C{// s is assigned ""}§
     2612\end{cfa}
     2613
     2614
     2615\subsection{Miscellaneous}
     2616
     2617The ©trim© operation
     2618\begin{cfa}
     2619string trim( string &mask, occurrence occ = first )
     2620\end{cfa}
     2621returns a string in that is the longest substring of leading or trailing characters (depending on the occurrence indicator, which is either ©first© or ©last©) which ARE included in the ©mask© are removed.
     2622\begin{cfa}
     2623// remove leading blanks
     2624s = string( "   ABC" ).trim( " " );                     §\C{// s is assigned "ABC",}§
     2625// remove trailing blanks
     2626s = string( "ABC   " ).trim( " ", last );       §\C{// s is assigned "ABC",}§
     2627\end{cfa}
     2628
     2629The ©translate© operation
     2630\begin{cfa}
     2631string translate( string &from, string &to )
     2632\end{cfa}
     2633returns a string that is the same length as the original string in which all occurrences of the characters that appear in the ©from© string have been translated into their corresponding character in the ©to© string.
     2634Translation is done on a character by character basis between the ©from© and ©to© strings; hence these two strings must be the same length.
     2635If a character in the original string does not appear in the ©from© string, then it simply appears as is in the resulting string.
     2636\begin{cfa}
     2637// upper to lower case
     2638peter = peter.translate( "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz" );
     2639                        // peter is assigned "peter"
     2640s = ifstmt.translate( "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz" );
     2641                        // ifstmt is assigned "if (a > b) {"
     2642// lower to upper case
     2643peter = peter.translate( "abcdefghijklmnopqrstuvwxyz", "ABCDEFGHIJKLMNOPQRSTUVWXYZ" );
     2644                        // peter is assigned "PETER"
     2645\end{cfa}
     2646
     2647The ©replace© operation
     2648\begin{cfa}
     2649string replace( string &from, string &to )
     2650\end{cfa}
     2651returns a string in which all occurrences of the ©from© string in the current string have been replaced by the ©to© string.
     2652\begin{cfa}
     2653s = peter.replace( "E", "XX" ); §\C{// s is assigned "PXXTXXR"}§
     2654\end{cfa}
     2655The replacement is done left-to-right.
     2656When an instance of the ©from© string is found and changed to the ©to© string, it is NOT examined again for further replacement.
     2657
     2658
     2659\section{Returning N+1 on Failure}
     2660
     2661Any of the string search routines can fail at some point during the search.
     2662When this happens it is necessary to return indicating the failure.
     2663Many string types in other languages use some special value to indicate the failure.
     2664This value is often 0 or -1 (PL/I returns 0).
     2665This section argues that a value of N+1, where N is the length of the base string in the search, is a more useful value to return.
     2666The index-of function in APL returns N+1.
     2667These are the boundary situations and are often overlooked when designing a string type.
     2668
     2669The situation that can be optimized by returning N+1 is when a search is performed to find the starting location for a substring operation.
     2670For example, in a program that is extracting words from a text file, it is necessary to scan from left to right over whitespace until the first alphabetic character is found.
     2671\begin{cfa}
     2672line = line( line.exclude( alpha ) );
     2673\end{cfa}
     2674If a text line contains all whitespaces, the exclude operation fails to find an alphabetic character.
     2675If ©exclude© returns 0 or -1, the result of the substring operation is unclear.
     2676Most string types generate an error, or clip the starting value to 1, resulting in the entire whitespace string being selected.
     2677If ©exclude© returns N+1, the starting position for the substring operation is beyond the end of the string leaving a null string.
     2678
     2679The same situation occurs when scanning off a word.
     2680\begin{cfa}
     2681start = line.include(alpha);
     2682word = line(1, start - 1);
     2683\end{cfa}
     2684If the entire line is composed of a word, the include operation will  fail to find a non-alphabetic character.
     2685In general, returning 0 or -1 is not an appropriate starting position for the substring, which must substring off the word leaving a null string.
     2686However, returning N+1 will substring off the word leaving a null string.
     2687
     2688
     2689\subsection{Input/Output Operators}
     2690
     2691Both the \CC operators ©<<© and ©>>© are defined on type ©string©.
     2692However, input of a string value is different from input of a ©char *© value.
     2693When a string value is read, \emph{all} input characters from the current point in the input stream to either the end of line (©'\n'©) or the end of file are read.
     2694
     2695
    23212696\section{Enumeration}
    23222697
     
    23322707Hence, enums may be overloaded with variable, enum, and function names.
    23332708\begin{cfa}
    2334 int Foo;                        $\C{// type/variable separate namespaces}$
     2709int Foo;                        §\C{// type/variable separate namespaces}§
    23352710enum Foo { Bar };
    2336 enum Goo { Bar };       $\C[1.75in]{// overload Foo.Bar}$
    2337 double Bar;                     $\C{// overload Foo.Bar, Goo.Bar}\CRT$
     2711enum Goo { Bar };       §\C[1.75in]{// overload Foo.Bar}§
     2712double Bar;                     §\C{// overload Foo.Bar, Goo.Bar}\CRT§
    23382713\end{cfa}
    23392714An anonymous enumeration injects enums with specific values into a scope.
     
    24082783The following examples illustrate the difference between the enumeration type and the type of its enums.
    24092784\begin{cfa}
    2410 Math m = PI;    $\C[1.5in]{// allowed}$
    2411 double d = PI;  $\C{// allowed, conversion to base type}$
    2412 m = E;                  $\C{// allowed}$
    2413 m = Alph;               $\C{// {\color{red}disallowed}}$
    2414 m = 3.141597;   $\C{// {\color{red}disallowed}}$
    2415 d = m;                  $\C{// allowed}$
    2416 d = Alph;               $\C{// {\color{red}disallowed}}$
    2417 Letter l = A;   $\C{// allowed}$
    2418 Greek g = Alph; $\C{// allowed}$
    2419 l = Alph;               $\C{// allowed, conversion to base type}$
    2420 g = A;                  $\C{// {\color{red}disallowed}}\CRT$
     2785Math m = PI;    §\C[1.5in]{// allowed}§
     2786double d = PI;  §\C{// allowed, conversion to base type}§
     2787m = E;                  §\C{// allowed}§
     2788m = Alph;               §\C{// {\color{red}disallowed}}§
     2789m = 3.141597;   §\C{// {\color{red}disallowed}}§
     2790d = m;                  §\C{// allowed}§
     2791d = Alph;               §\C{// {\color{red}disallowed}}§
     2792Letter l = A;   §\C{// allowed}§
     2793Greek g = Alph; §\C{// allowed}§
     2794l = Alph;               §\C{// allowed, conversion to base type}§
     2795g = A;                  §\C{// {\color{red}disallowed}}\CRT§
    24212796\end{cfa}
    24222797
     
    25082883\begin{cfa}
    25092884®[ int o1, int o2, char o3 ]® f( int i1, char i2, char i3 ) {
    2510         $\emph{routine body}$
     2885        §\emph{routine body}§
    25112886}
    25122887\end{cfa}
     
    25192894Declaration qualifiers can only appear at the start of a routine definition, \eg:
    25202895\begin{cfa}
    2521 ®extern® [ int x ] g( int y ) {$\,$}
     2896®extern® [ int x ] g( int y ) {§\,§}
    25222897\end{cfa}
    25232898Lastly, if there are no output parameters or input parameters, the brackets and/or parentheses must still be specified;
    25242899in both cases the type is assumed to be void as opposed to old style C defaults of int return type and unknown parameter types, respectively, as in:
    25252900\begin{cfa}
    2526 [$\,$] g(); $\C{// no input or output parameters}$
    2527 [ void ] g( void ); $\C{// no input or output parameters}$
     2901[§\,§] g(); §\C{// no input or output parameters}§
     2902[ void ] g( void ); §\C{// no input or output parameters}§
    25282903\end{cfa}
    25292904
     
    25432918\begin{cfa}
    25442919typedef int foo;
    2545 int f( int (* foo) ); $\C{// foo is redefined as a parameter name}$
     2920int f( int (* foo) ); §\C{// foo is redefined as a parameter name}§
    25462921\end{cfa}
    25472922The string ``©int (* foo)©'' declares a C-style named-parameter of type pointer to an integer (the parenthesis are superfluous), while the same string declares a \CFA style unnamed parameter of type routine returning integer with unnamed parameter of type pointer to foo.
     
    25512926C-style declarations can be used to declare parameters for \CFA style routine definitions, \eg:
    25522927\begin{cfa}
    2553 [ int ] f( * int, int * ); $\C{// returns an integer, accepts 2 pointers to integers}$
    2554 [ * int, int * ] f( int ); $\C{// returns 2 pointers to integers, accepts an integer}$
     2928[ int ] f( * int, int * ); §\C{// returns an integer, accepts 2 pointers to integers}§
     2929[ * int, int * ] f( int ); §\C{// returns 2 pointers to integers, accepts an integer}§
    25552930\end{cfa}
    25562931The reason for allowing both declaration styles in the new context is for backwards compatibility with existing preprocessor macros that generate C-style declaration-syntax, as in:
    25572932\begin{cfa}
    25582933#define ptoa( n, d ) int (*n)[ d ]
    2559 int f( ptoa( p, 5 ) ) ... $\C{// expands to int f( int (*p)[ 5 ] )}$
    2560 [ int ] f( ptoa( p, 5 ) ) ... $\C{// expands to [ int ] f( int (*p)[ 5 ] )}$
     2934int f( ptoa( p, 5 ) ) ... §\C{// expands to int f( int (*p)[ 5 ] )}§
     2935[ int ] f( ptoa( p, 5 ) ) ... §\C{// expands to [ int ] f( int (*p)[ 5 ] )}§
    25612936\end{cfa}
    25622937Again, programmers are highly encouraged to use one declaration form or the other, rather than mixing the forms.
     
    25802955        int z;
    25812956        ... x = 0; ... y = z; ...
    2582         ®return;® $\C{// implicitly return x, y}$
     2957        ®return;® §\C{// implicitly return x, y}§
    25832958}
    25842959\end{cfa}
     
    25902965[ int x, int y ] f() {
    25912966        ...
    2592 } $\C{// implicitly return x, y}$
     2967} §\C{// implicitly return x, y}§
    25932968\end{cfa}
    25942969In this case, the current values of ©x© and ©y© are returned to the calling routine just as if a ©return© had been encountered.
     
    25992974[ int x, int y ] f( int, x, int y ) {
    26002975        ...
    2601 } $\C{// implicitly return x, y}$
     2976} §\C{// implicitly return x, y}§
    26022977\end{cfa}
    26032978This notation allows the compiler to eliminate temporary variables in nested routine calls.
    26042979\begin{cfa}
    2605 [ int x, int y ] f( int, x, int y ); $\C{// prototype declaration}$
     2980[ int x, int y ] f( int, x, int y ); §\C{// prototype declaration}§
    26062981int a, b;
    26072982[a, b] = f( f( f( a, b ) ) );
     
    26172992as well, parameter names are optional, \eg:
    26182993\begin{cfa}
    2619 [ int x ] f (); $\C{// returning int with no parameters}$
    2620 [ * int ] g (int y); $\C{// returning pointer to int with int parameter}$
    2621 [ ] h ( int, char ); $\C{// returning no result with int and char parameters}$
    2622 [ * int, int ] j ( int ); $\C{// returning pointer to int and int, with int parameter}$
     2994[ int x ] f (); §\C{// returning int with no parameters}§
     2995[ * int ] g (int y); §\C{// returning pointer to int with int parameter}§
     2996[ ] h ( int, char ); §\C{// returning no result with int and char parameters}§
     2997[ * int, int ] j ( int ); §\C{// returning pointer to int and int, with int parameter}§
    26232998\end{cfa}
    26242999This syntax allows a prototype declaration to be created by cutting and pasting source text from the routine definition header (or vice versa).
     
    26263001\begin{cfa}
    26273002C :             const double bar1(), bar2( int ), bar3( double );
    2628 $\CFA$: [const double] foo(), foo( int ), foo( double ) { return 3.0; }
     3003§\CFA§: [const double] foo(), foo( int ), foo( double ) { return 3.0; }
    26293004\end{cfa}
    26303005\CFA allows the last routine in the list to define its body.
     
    26413016The syntax for pointers to \CFA routines specifies the pointer name on the right, \eg:
    26423017\begin{cfa}
    2643 * [ int x ] () fp; $\C[2.25in]{// pointer to routine returning int with no parameters}$
    2644 * [ * int ] (int y) gp; $\C{// pointer to routine returning pointer to int with int parameter}$
    2645 * [ ] (int,char) hp; $\C{// pointer to routine returning no result with int and char parameters}$
    2646 * [ * int,int ] ( int ) jp; $\C{// pointer to routine returning pointer to int and int, with int parameter}\CRT$
     3018* [ int x ] () fp; §\C[2.25in]{// pointer to routine returning int with no parameters}§
     3019* [ * int ] (int y) gp; §\C{// pointer to routine returning pointer to int with int parameter}§
     3020* [ ] (int,char) hp; §\C{// pointer to routine returning no result with int and char parameters}§
     3021* [ * int,int ] ( int ) jp; §\C{// pointer to routine returning pointer to int and int, with int parameter}\CRT§
    26473022\end{cfa}
    26483023While parameter names are optional, \emph{a routine name cannot be specified};
    26493024for example, the following is incorrect:
    26503025\begin{cfa}
    2651 * [ int x ] f () fp; $\C{// routine name "f" is not allowed}$
     3026* [ int x ] f () fp; §\C{// routine name "f" is not allowed}§
    26523027\end{cfa}
    26533028
     
    26723047whereas a named (keyword) call may be:
    26733048\begin{cfa}
    2674 p( z : 3, x : 4, y : 7 );  $\C{// rewrite \(\Rightarrow\) p( 4, 7, 3 )}$
     3049p( z : 3, x : 4, y : 7 );  §\C{// rewrite \(\Rightarrow\) p( 4, 7, 3 )}§
    26753050\end{cfa}
    26763051Here the order of the arguments is unimportant, and the names of the parameters are used to associate argument values with the corresponding parameters.
     
    26893064For example, the following routine prototypes and definition are all valid.
    26903065\begin{cfa}
    2691 void p( int, int, int ); $\C{// equivalent prototypes}$
     3066void p( int, int, int ); §\C{// equivalent prototypes}§
    26923067void p( int x, int y, int z );
    26933068void p( int y, int x, int z );
    26943069void p( int z, int y, int x );
    2695 void p( int q, int r, int s ) {} $\C{// match with this definition}$
     3070void p( int q, int r, int s ) {} §\C{// match with this definition}§
    26963071\end{cfa}
    26973072Forcing matching parameter names in routine prototypes with corresponding routine definitions is possible, but goes against a strong tradition in C programming.
     
    27053080int f( int x, double y );
    27063081
    2707 f( j : 3, i : 4 ); $\C{// 1st f}$
    2708 f( x : 7, y : 8.1 ); $\C{// 2nd f}$
    2709 f( 4, 5 );  $\C{// ambiguous call}$
     3082f( j : 3, i : 4 ); §\C{// 1st f}§
     3083f( x : 7, y : 8.1 ); §\C{// 2nd f}§
     3084f( 4, 5 );  §\C{// ambiguous call}§
    27103085\end{cfa}
    27113086However, named arguments compound routine resolution in conjunction with conversions:
    27123087\begin{cfa}
    2713 f( i : 3, 5.7 ); $\C{// ambiguous call ?}$
     3088f( i : 3, 5.7 ); §\C{// ambiguous call ?}§
    27143089\end{cfa}
    27153090Depending on the cost associated with named arguments, this call could be resolvable or ambiguous.
     
    27253100the allowable positional calls are:
    27263101\begin{cfa}
    2727 p(); $\C{// rewrite \(\Rightarrow\) p( 1, 2, 3 )}$
    2728 p( 4 ); $\C{// rewrite \(\Rightarrow\) p( 4, 2, 3 )}$
    2729 p( 4, 4 ); $\C{// rewrite \(\Rightarrow\) p( 4, 4, 3 )}$
    2730 p( 4, 4, 4 ); $\C{// rewrite \(\Rightarrow\) p( 4, 4, 4 )}$
     3102p(); §\C{// rewrite \(\Rightarrow\) p( 1, 2, 3 )}§
     3103p( 4 ); §\C{// rewrite \(\Rightarrow\) p( 4, 2, 3 )}§
     3104p( 4, 4 ); §\C{// rewrite \(\Rightarrow\) p( 4, 4, 3 )}§
     3105p( 4, 4, 4 ); §\C{// rewrite \(\Rightarrow\) p( 4, 4, 4 )}§
    27313106// empty arguments
    2732 p(  , 4, 4 ); $\C{// rewrite \(\Rightarrow\) p( 1, 4, 4 )}$
    2733 p( 4,  , 4 ); $\C{// rewrite \(\Rightarrow\) p( 4, 2, 4 )}$
    2734 p( 4, 4,   ); $\C{// rewrite \(\Rightarrow\) p( 4, 4, 3 )}$
    2735 p( 4,  ,   ); $\C{// rewrite \(\Rightarrow\) p( 4, 2, 3 )}$
    2736 p(  , 4,   ); $\C{// rewrite \(\Rightarrow\) p( 1, 4, 3 )}$
    2737 p(  ,  , 4 ); $\C{// rewrite \(\Rightarrow\) p( 1, 2, 4 )}$
    2738 p(  ,  ,   ); $\C{// rewrite \(\Rightarrow\) p( 1, 2, 3 )}$
     3107p(  , 4, 4 ); §\C{// rewrite \(\Rightarrow\) p( 1, 4, 4 )}§
     3108p( 4,  , 4 ); §\C{// rewrite \(\Rightarrow\) p( 4, 2, 4 )}§
     3109p( 4, 4,   ); §\C{// rewrite \(\Rightarrow\) p( 4, 4, 3 )}§
     3110p( 4,  ,   ); §\C{// rewrite \(\Rightarrow\) p( 4, 2, 3 )}§
     3111p(  , 4,   ); §\C{// rewrite \(\Rightarrow\) p( 1, 4, 3 )}§
     3112p(  ,  , 4 ); §\C{// rewrite \(\Rightarrow\) p( 1, 2, 4 )}§
     3113p(  ,  ,   ); §\C{// rewrite \(\Rightarrow\) p( 1, 2, 3 )}§
    27393114\end{cfa}
    27403115Here the missing arguments are inserted from the default values in the parameter list.
     
    27603135Default values may only appear in a prototype versus definition context:
    27613136\begin{cfa}
    2762 void p( int x, int y = 2, int z = 3 ); $\C{// prototype: allowed}$
    2763 void p( int, int = 2, int = 3 ); $\C{// prototype: allowed}$
    2764 void p( int x, int y = 2, int z = 3 ) {} $\C{// definition: not allowed}$
     3137void p( int x, int y = 2, int z = 3 ); §\C{// prototype: allowed}§
     3138void p( int, int = 2, int = 3 ); §\C{// prototype: allowed}§
     3139void p( int x, int y = 2, int z = 3 ) {} §\C{// definition: not allowed}§
    27653140\end{cfa}
    27663141The reason for this restriction is to allow separate compilation.
     
    27773152\begin{cfa}
    27783153p( int x, int y, int z, ... );
    2779 p( 1, 4, 5, 6, z : 3, y : 2 ); $\C{// assume p( /* positional */, ... , /* named */ );}$
    2780 p( 1, z : 3, y : 2, 4, 5, 6 ); $\C{// assume p( /* positional */, /* named */, ... );}$
     3154p( 1, 4, 5, 6, z : 3, y : 2 ); §\C{// assume p( /* positional */, ... , /* named */ );}§
     3155p( 1, z : 3, y : 2, 4, 5, 6 ); §\C{// assume p( /* positional */, /* named */, ... );}§
    27813156\end{cfa}
    27823157In the first call, it is necessary for the programmer to conceptually rewrite the call, changing named arguments into positional, before knowing where the ellipse arguments begin.
     
    27873162\begin{cfa}
    27883163void p( int x, int y = 2, int z = 3... );
    2789 p( 1, 4, 5, 6, z : 3 ); $\C{// assume p( /* positional */, ... , /* named */ );}$
    2790 p( 1, z : 3, 4, 5, 6 ); $\C{// assume p( /* positional */, /* named */, ... );}$
     3164p( 1, 4, 5, 6, z : 3 ); §\C{// assume p( /* positional */, ... , /* named */ );}§
     3165p( 1, z : 3, 4, 5, 6 ); §\C{// assume p( /* positional */, /* named */, ... );}§
    27913166\end{cfa}
    27923167The first call is an error because arguments 4 and 5 are actually positional not ellipse arguments;
     
    28183193Furthermore, overloading cannot handle accessing default arguments in the middle of a positional list, via a missing argument, such as:
    28193194\begin{cfa}
    2820 p( 1, /* default */, 5 ); $\C{// rewrite \(\Rightarrow\) p( 1, 2, 5 )}$
     3195p( 1, /* default */, 5 ); §\C{// rewrite \(\Rightarrow\) p( 1, 2, 5 )}§
    28213196\end{cfa}
    28223197
     
    28313206\begin{cfa}
    28323207struct {
    2833         int f1; $\C{// named field}$
    2834         int f2 : 4; $\C{// named field with bit field size}$
    2835         int : 3; $\C{// unnamed field for basic type with bit field size}$
    2836         int ; $\C{// disallowed, unnamed field}$
    2837         int *; $\C{// disallowed, unnamed field}$
    2838         int (*)( int ); $\C{// disallowed, unnamed field}$
     3208        int f1; §\C{// named field}§
     3209        int f2 : 4; §\C{// named field with bit field size}§
     3210        int : 3; §\C{// unnamed field for basic type with bit field size}§
     3211        int ; §\C{// disallowed, unnamed field}§
     3212        int *; §\C{// disallowed, unnamed field}§
     3213        int (*)( int ); §\C{// disallowed, unnamed field}§
    28393214};
    28403215\end{cfa}
     
    28443219\begin{cfa}
    28453220struct {
    2846         int , , ; $\C{// 3 unnamed fields}$
     3221        int , , ; §\C{// 3 unnamed fields}§
    28473222}
    28483223\end{cfa}
     
    29383313const unsigned int size = 5;
    29393314int ia[size];
    2940 ... $\C{// assign values to array ia}$
    2941 qsort( ia, size ); $\C{// sort ascending order using builtin ?<?}$
     3315... §\C{// assign values to array ia}§
     3316qsort( ia, size ); §\C{// sort ascending order using builtin ?<?}§
    29423317{
    2943         ®int ?<?( int x, int y ) { return x > y; }® $\C{// nested routine}$
    2944         qsort( ia, size ); $\C{// sort descending order by local redefinition}$
     3318        ®int ?<?( int x, int y ) { return x > y; }® §\C{// nested routine}§
     3319        qsort( ia, size ); §\C{// sort descending order by local redefinition}§
    29453320}
    29463321\end{cfa}
     
    29503325The following program in undefined in \CFA (and Indexc{gcc})
    29513326\begin{cfa}
    2952 [* [int]( int )] foo() { $\C{// int (* foo())( int )}$
     3327[* [int]( int )] foo() { §\C{// int (* foo())( int )}§
    29533328        int ®i® = 7;
    29543329        int bar( int p ) {
    2955                 ®i® += 1; $\C{// dependent on local variable}$
     3330                ®i® += 1; §\C{// dependent on local variable}§
    29563331                sout | ®i®;
    29573332        }
    2958         return bar; $\C{// undefined because of local dependence}$
     3333        return bar; §\C{// undefined because of local dependence}§
    29593334}
    29603335int main() {
    2961         * [int]( int ) fp = foo(); $\C{// int (* fp)( int )}$
     3336        * [int]( int ) fp = foo(); §\C{// int (* fp)( int )}§
    29623337        sout | fp( 3 );
    29633338}
     
    29723347In C and \CFA, lists of elements appear in several contexts, such as the parameter list of a routine call.
    29733348\begin{cfa}
    2974 f( ®2, x, 3 + i® ); $\C{// element list}$
     3349f( ®2, x, 3 + i® ); §\C{// element list}§
    29753350\end{cfa}
    29763351A list of elements is called a \newterm{tuple}, and is different from a \Index{comma expression}.
     
    29873362For example, consider C's \Indexc{div} function, which returns the quotient and remainder for a division of an integer value.
    29883363\begin{cfa}
    2989 typedef struct { int quot, rem; } div_t;        $\C[7cm]{// from include stdlib.h}$
     3364typedef struct { int quot, rem; } div_t;        §\C[7cm]{// from include stdlib.h}§
    29903365div_t div( int num, int den );
    2991 div_t qr = div( 13, 5 ); $\C{// return quotient/remainder aggregate}$
    2992 printf( "%d %d\n", qr.quot, qr.rem ); $\C{// print quotient/remainder}$
     3366div_t qr = div( 13, 5 ); §\C{// return quotient/remainder aggregate}§
     3367printf( "%d %d\n", qr.quot, qr.rem ); §\C{// print quotient/remainder}§
    29933368\end{cfa}
    29943369This approach requires a name for the return type and fields, where \Index{naming} is a common programming-language issue.
     
    30003375For example, consider C's \Indexc{modf} function, which returns the integral and fractional part of a floating value.
    30013376\begin{cfa}
    3002 double modf( double x, double * i ); $\C{// from include math.h}$
    3003 double intp, frac = modf( 13.5, &intp ); $\C{// return integral and fractional components}$
    3004 printf( "%g %g\n", intp, frac ); $\C{// print integral/fractional components}$
     3377double modf( double x, double * i ); §\C{// from include math.h}§
     3378double intp, frac = modf( 13.5, &intp ); §\C{// return integral and fractional components}§
     3379printf( "%g %g\n", intp, frac ); §\C{// print integral/fractional components}§
    30053380\end{cfa}
    30063381This approach requires allocating storage for the return values, which complicates the call site with a sequence of variable declarations leading to the call.
     
    30293404When a function call is passed as an argument to another call, the best match of actual arguments to formal parameters is evaluated given all possible expression interpretations in the current scope.
    30303405\begin{cfa}
    3031 void g( int, int ); $\C{// 1}$
    3032 void g( double, double ); $\C{// 2}$
    3033 g( div( 13, 5 ) ); $\C{// select 1}$
    3034 g( modf( 13.5 ) ); $\C{// select 2}$
     3406void g( int, int ); §\C{// 1}§
     3407void g( double, double ); §\C{// 2}§
     3408g( div( 13, 5 ) ); §\C{// select 1}§
     3409g( modf( 13.5 ) ); §\C{// select 2}§
    30353410\end{cfa}
    30363411In this case, there are two overloaded ©g© routines.
     
    30413416The previous examples can be rewritten passing the multiple returned-values directly to the ©printf© function call.
    30423417\begin{cfa}
    3043 [ int, int ] div( int x, int y ); $\C{// from include stdlib}$
    3044 printf( "%d %d\n", div( 13, 5 ) ); $\C{// print quotient/remainder}$
    3045 
    3046 [ double, double ] modf( double x ); $\C{// from include math}$
    3047 printf( "%g %g\n", modf( 13.5 ) ); $\C{// print integral/fractional components}$
     3418[ int, int ] div( int x, int y ); §\C{// from include stdlib}§
     3419printf( "%d %d\n", div( 13, 5 ) ); §\C{// print quotient/remainder}§
     3420
     3421[ double, double ] modf( double x ); §\C{// from include math}§
     3422printf( "%g %g\n", modf( 13.5 ) ); §\C{// print integral/fractional components}§
    30483423\end{cfa}
    30493424This approach provides the benefits of compile-time checking for appropriate return statements as in aggregation, but without the required verbosity of declaring a new named type.
     
    30553430\begin{cfa}
    30563431int quot, rem;
    3057 [ quot, rem ] = div( 13, 5 ); $\C{// assign multiple variables}$
    3058 printf( "%d %d\n", quot, rem ); $\C{// print quotient/remainder}\CRT$
     3432[ quot, rem ] = div( 13, 5 ); §\C{// assign multiple variables}§
     3433printf( "%d %d\n", quot, rem ); §\C{// print quotient/remainder}\CRT§
    30593434\end{cfa}
    30603435Here, the multiple return-values are matched in much the same way as passing multiple return-values to multiple parameters in a call.
     
    30853460In \CFA, it is possible to overcome this restriction by declaring a \newterm{tuple variable}.
    30863461\begin{cfa}
    3087 [int, int] ®qr® = div( 13, 5 ); $\C{// initialize tuple variable}$
    3088 printf( "%d %d\n", ®qr® ); $\C{// print quotient/remainder}$
     3462[int, int] ®qr® = div( 13, 5 ); §\C{// initialize tuple variable}§
     3463printf( "%d %d\n", ®qr® ); §\C{// print quotient/remainder}§
    30893464\end{cfa}
    30903465It is now possible to match the multiple return-values to a single variable, in much the same way as \Index{aggregation}.
     
    30923467One way to access the individual components of a tuple variable is with assignment.
    30933468\begin{cfa}
    3094 [ quot, rem ] = qr; $\C{// assign multiple variables}$
     3469[ quot, rem ] = qr; §\C{// assign multiple variables}§
    30953470\end{cfa}
    30963471
     
    31153490[int, double] * p;
    31163491
    3117 int y = x.0; $\C{// access int component of x}$
    3118 y = f().1; $\C{// access int component of f}$
    3119 p->0 = 5; $\C{// access int component of tuple pointed-to by p}$
    3120 g( x.1, x.0 ); $\C{// rearrange x to pass to g}$
    3121 double z = [ x, f() ].0.1; $\C{// access second component of first component of tuple expression}$
     3492int y = x.0; §\C{// access int component of x}§
     3493y = f().1; §\C{// access int component of f}§
     3494p->0 = 5; §\C{// access int component of tuple pointed-to by p}§
     3495g( x.1, x.0 ); §\C{// rearrange x to pass to g}§
     3496double z = [ x, f() ].0.1; §\C{// access second component of first component of tuple expression}§
    31223497\end{cfa}
    31233498Tuple-index expressions can occur on any tuple-typed expression, including tuple-returning functions, square-bracketed tuple expressions, and other tuple-index expressions, provided the retrieved component is also a tuple.
     
    31873562double y;
    31883563[int, double] z;
    3189 [y, x] = 3.14; $\C{// mass assignment}$
    3190 [x, y] = z;                                                         $\C{// multiple assignment}$
    3191 z = 10;                                                         $\C{// mass assignment}$
    3192 z = [x, y]; $\C{// multiple assignment}$
     3564[y, x] = 3.14; §\C{// mass assignment}§
     3565[x, y] = z;                                                         §\C{// multiple assignment}§
     3566z = 10;                                                         §\C{// mass assignment}§
     3567z = [x, y]; §\C{// multiple assignment}§
    31933568\end{cfa}
    31943569Let $L_i$ for $i$ in $[0, n)$ represent each component of the flattened left side, $R_i$ represent each component of the flattened right side of a multiple assignment, and $R$ represent the right side of a mass assignment.
     
    31983573\begin{cfa}
    31993574[ int, int ] x, y, z;
    3200 [ x, y ] = z;                                              $\C{// multiple assignment, invalid 4 != 2}$
     3575[ x, y ] = z;                                              §\C{// multiple assignment, invalid 4 != 2}§
    32013576\end{cfa}
    32023577Multiple assignment assigns $R_i$ to $L_i$ for each $i$.
     
    32343609        double c, d;
    32353610        [ void ] f( [ int, int ] );
    3236         f( [ c, a ] = [ b, d ] = 1.5 ); $\C{// assignments in parameter list}$
     3611        f( [ c, a ] = [ b, d ] = 1.5 ); §\C{// assignments in parameter list}§
    32373612\end{cfa}
    32383613The tuple expression begins with a mass assignment of ©1.5© into ©[b, d]©, which assigns ©1.5© into ©b©, which is truncated to ©1©, and ©1.5© into ©d©, producing the tuple ©[1, 1.5]© as a result.
     
    32473622\begin{cfa}
    32483623struct S;
    3249 void ?{}(S *); $\C{// (1)}$
    3250 void ?{}(S *, int); $\C{// (2)}$
    3251 void ?{}(S * double); $\C{// (3)}$
    3252 void ?{}(S *, S); $\C{// (4)}$
    3253 
    3254 [S, S] x = [3, 6.28]; $\C{// uses (2), (3), specialized constructors}$
    3255 [S, S] y; $\C{// uses (1), (1), default constructor}$
    3256 [S, S] z = x.0; $\C{// uses (4), (4), copy constructor}$
     3624void ?{}(S *); §\C{// (1)}§
     3625void ?{}(S *, int); §\C{// (2)}§
     3626void ?{}(S * double); §\C{// (3)}§
     3627void ?{}(S *, S); §\C{// (4)}§
     3628
     3629[S, S] x = [3, 6.28]; §\C{// uses (2), (3), specialized constructors}§
     3630[S, S] y; §\C{// uses (1), (1), default constructor}§
     3631[S, S] z = x.0; §\C{// uses (4), (4), copy constructor}§
    32573632\end{cfa}
    32583633In this example, ©x© is initialized by the multiple constructor calls ©?{}(&x.0, 3)© and ©?{}(&x.1, 6.28)©, while ©y© is initialized by two default constructor calls ©?{}(&y.0)© and ©?{}(&y.1)©.
     
    32953670A member-access tuple may be used anywhere a tuple can be used, \eg:
    32963671\begin{cfa}
    3297 s.[ y, z, x ] = [ 3, 3.2, 'x' ]; $\C{// equivalent to s.x = 'x', s.y = 3, s.z = 3.2}$
    3298 f( s.[ y, z ] ); $\C{// equivalent to f( s.y, s.z )}$
     3672s.[ y, z, x ] = [ 3, 3.2, 'x' ]; §\C{// equivalent to s.x = 'x', s.y = 3, s.z = 3.2}§
     3673f( s.[ y, z ] ); §\C{// equivalent to f( s.y, s.z )}§
    32993674\end{cfa}
    33003675Note, the fields appearing in a record-field tuple may be specified in any order;
     
    33063681void f( double, long );
    33073682
    3308 f( x.[ 0, 3 ] ); $\C{// f( x.0, x.3 )}$
    3309 x.[ 0, 1 ] = x.[ 1, 0 ]; $\C{// [ x.0, x.1 ] = [ x.1, x.0 ]}$
     3683f( x.[ 0, 3 ] ); §\C{// f( x.0, x.3 )}§
     3684x.[ 0, 1 ] = x.[ 1, 0 ]; §\C{// [ x.0, x.1 ] = [ x.1, x.0 ]}§
    33103685[ long, int, long ] y = x.[ 2, 0, 2 ];
    33113686\end{cfa}
     
    33243699\begin{cfa}
    33253700[ int, float, double ] f();
    3326 [ double, float ] x = f().[ 2, 1 ]; $\C{// f() called once}$
     3701[ double, float ] x = f().[ 2, 1 ]; §\C{// f() called once}§
    33273702\end{cfa}
    33283703
     
    33373712That is, a cast can be used to select the type of an expression when it is ambiguous, as in the call to an overloaded function.
    33383713\begin{cfa}
    3339 int f(); $\C{// (1)}$
    3340 double f(); $\C{// (2)}$
    3341 
    3342 f(); $\C{// ambiguous - (1),(2) both equally viable}$
    3343 (int)f(); $\C{// choose (2)}$
     3714int f(); §\C{// (1)}§
     3715double f(); §\C{// (2)}§
     3716
     3717f(); §\C{// ambiguous - (1),(2) both equally viable}§
     3718(int)f(); §\C{// choose (2)}§
    33443719\end{cfa}
    33453720Since casting is a fundamental operation in \CFA, casts need to be given a meaningful interpretation in the context of tuples.
     
    33493724void g();
    33503725
    3351 (void)f(); $\C{// valid, ignore results}$
    3352 (int)g(); $\C{// invalid, void cannot be converted to int}$
     3726(void)f(); §\C{// valid, ignore results}§
     3727(int)g(); §\C{// invalid, void cannot be converted to int}§
    33533728
    33543729struct A { int x; };
    3355 (struct A)f(); $\C{// invalid, int cannot be converted to A}$
     3730(struct A)f(); §\C{// invalid, int cannot be converted to A}§
    33563731\end{cfa}
    33573732In C, line 4 is a valid cast, which calls ©f© and discards its result.
     
    33693744        [int, [int, int], int] g();
    33703745
    3371         ([int, double])f(); $\C{// (1) valid}$
    3372         ([int, int, int])g(); $\C{// (2) valid}$
    3373         ([void, [int, int]])g(); $\C{// (3) valid}$
    3374         ([int, int, int, int])g(); $\C{// (4) invalid}$
    3375         ([int, [int, int, int]])g(); $\C{// (5) invalid}$
     3746        ([int, double])f(); §\C{// (1) valid}§
     3747        ([int, int, int])g(); §\C{// (2) valid}§
     3748        ([void, [int, int]])g(); §\C{// (3) valid}§
     3749        ([int, int, int, int])g(); §\C{// (4) invalid}§
     3750        ([int, [int, int, int]])g(); §\C{// (5) invalid}§
    33763751\end{cfa}
    33773752
     
    34333808void f([int, int], int, int);
    34343809
    3435 f([0, 0], 0, 0); $\C{// no cost}$
    3436 f(0, 0, 0, 0); $\C{// cost for structuring}$
    3437 f([0, 0,], [0, 0]); $\C{// cost for flattening}$
    3438 f([0, 0, 0], 0); $\C{// cost for flattening and structuring}$
     3810f([0, 0], 0, 0); §\C{// no cost}§
     3811f(0, 0, 0, 0); §\C{// cost for structuring}§
     3812f([0, 0,], [0, 0]); §\C{// cost for flattening}§
     3813f([0, 0, 0], 0); §\C{// cost for flattening and structuring}§
    34393814\end{cfa}
    34403815
     
    35003875[ unsigned int, char ]
    35013876[ double, double, double ]
    3502 [ * int, int * ] $\C{// mix of CFA and ANSI}$
     3877[ * int, int * ] §\C{// mix of CFA and ANSI}§
    35033878[ * [ 5 ] int, * * char, * [ [ int, int ] ] (int, int) ]
    35043879\end{cfa}
     
    35073882Examples of declarations using tuple types are:
    35083883\begin{cfa}
    3509 [ int, int ] x; $\C{// 2 element tuple, each element of type int}$
    3510 * [ char, char ] y; $\C{// pointer to a 2 element tuple}$
     3884[ int, int ] x; §\C{// 2 element tuple, each element of type int}§
     3885* [ char, char ] y; §\C{// pointer to a 2 element tuple}§
    35113886[ [ int, int ] ] z ([ int, int ]);
    35123887\end{cfa}
     
    35253900[ int, int ] w1;
    35263901[ int, int, int ] w2;
    3527 [ void ] f (int, int, int); $\C{// three input parameters of type int}$
    3528 [ void ] g ([ int, int, int ]); $\C{3 element tuple as input}$
     3902[ void ] f (int, int, int); §\C{// three input parameters of type int}§
     3903[ void ] g ([ int, int, int ]); §\C{3 element tuple as input}§
    35293904f( [ 1, 2, 3 ] );
    35303905f( w1, 3 );
     
    36073982[ int, int, int, int ] w = [ 1, 2, 3, 4 ];
    36083983int x = 5;
    3609 [ x, w ] = [ w, x ]; $\C{// all four tuple coercions}$
     3984[ x, w ] = [ w, x ]; §\C{// all four tuple coercions}§
    36103985\end{cfa}
    36113986Starting on the right-hand tuple in the last assignment statement, w is opened, producing a tuple of four values;
     
    36974072both these examples produce indeterminate results:
    36984073\begin{cfa}
    3699 f( x++, x++ ); $\C{// C routine call with side effects in arguments}$
    3700 [ v1, v2 ] = [ x++, x++ ]; $\C{// side effects in right-hand side of multiple assignment}$
     4074f( x++, x++ ); §\C{// C routine call with side effects in arguments}§
     4075[ v1, v2 ] = [ x++, x++ ]; §\C{// side effects in right-hand side of multiple assignment}§
    37014076\end{cfa}
    37024077
     
    37974172\begin{cfa}
    37984173[int, [ int, int ] ] t1 = [ 1, [ 2, 3 ] ], t2 = [ 4, [ 5, 6 ] ];
    3799 sout | t1 | t2; $\C{// print tuples}$
     4174sout | t1 | t2; §\C{// print tuples}§
    38004175\end{cfa}
    38014176\begin{cfa}[showspaces=true,aboveskip=0pt]
     
    38814256
    38824257int main( int argc, char * argv[] ) {
    3883         ®ifstream® in  = stdin;                                 $\C{// copy default files}$
     4258        ®ifstream® in  = stdin;                                 §\C{// copy default files}§
    38844259        ®ofstream® out = stdout;
    38854260
     
    38874262                choose ( argc ) {
    38884263                  case 2, 3:
    3889                         ®open®( in, argv[1] );                  $\C{// open input file first as output creates file}$
    3890                         if ( argc == 3 ) ®open®( out, argv[2] ); $\C{// do not create output unless input opens}$
    3891                   case 1: ;                                                     $\C{// use default files}$
     4264                        ®open®( in, argv[1] );                  §\C{// open input file first as output creates file}§
     4265                        if ( argc == 3 ) ®open®( out, argv[2] ); §\C{// do not create output unless input opens}§
     4266                  case 1: ;                                                     §\C{// use default files}§
    38924267                  default:
    38934268                        ®exit® | "Usage" | argv[0] | "[ input-file (default stdin) "
    38944269                                   "[ output-file (default stdout) ] ]";
    38954270                } // choose
    3896         } catch( ®open_failure® * ex; ex->istream == &in ) { $\C{// input file errors}$
     4271        } catch( ®open_failure® * ex; ex->istream == &in ) { §\C{// input file errors}§
    38974272                ®exit® | "Unable to open input file" | argv[1];
    3898         } catch( ®open_failure® * ex; ex->ostream == &out ) { $\C{// output file errors}$
    3899                 ®close®( in );                                          $\C{// optional}$
     4273        } catch( ®open_failure® * ex; ex->ostream == &out ) { §\C{// output file errors}§
     4274                ®close®( in );                                          §\C{// optional}§
    39004275                ®exit® | "Unable to open output file" | argv[2];
    39014276        } // try
    39024277
    3903         out | nlOff;                                                    $\C{// turn off auto newline}$
    3904         in | nlOn;                                                              $\C{// turn on reading newline}$
     4278        out | nlOff;                                                    §\C{// turn off auto newline}§
     4279        in | nlOn;                                                              §\C{// turn on reading newline}§
    39054280        char ch;
    3906         for () {                                                                $\C{// read/write characters}$
     4281        for () {                                                                §\C{// read/write characters}§
    39074282                in | ch;
    3908           if ( eof( in ) ) break;                               $\C{// eof ?}$
     4283          if ( eof( in ) ) break;                               §\C{// eof ?}§
    39094284                out | ch;
    39104285        } // for
     
    39534328// *********************************** ofstream ***********************************
    39544329
    3955 bool fail( ofstream & );$\indexc{fail}\index{ofstream@©ofstream©!©fail©}$
    3956 void clear( ofstream & );$\indexc{clear}\index{ofstream@©ofstream©!©clear©}$
    3957 int flush( ofstream & );$\indexc{flush}\index{ofstream@©ofstream©!©flush©}$
    3958 void open( ofstream &, const char name[], const char mode[] = "w" );$\indexc{open}\index{ofstream@©ofstream©!©open©}$
    3959 void close( ofstream & );$\indexc{close}\index{ofstream@©ofstream©!©close©}$
    3960 ofstream & write( ofstream &, const char data[], size_t size );$\indexc{write}\index{ofstream@©ofstream©!©write©}$
    3961 
    3962 void ?{}( ofstream & );$\index{ofstream@©ofstream©!©?{}©}$
     4330bool fail( ofstream & );§\indexc{fail}\index{ofstream@©ofstream©!©fail©}§
     4331void clear( ofstream & );§\indexc{clear}\index{ofstream@©ofstream©!©clear©}§
     4332int flush( ofstream & );§\indexc{flush}\index{ofstream@©ofstream©!©flush©}§
     4333void open( ofstream &, const char name[], const char mode[] = "w" );§\indexc{open}\index{ofstream@©ofstream©!©open©}§
     4334void close( ofstream & );§\indexc{close}\index{ofstream@©ofstream©!©close©}§
     4335ofstream & write( ofstream &, const char data[], size_t size );§\indexc{write}\index{ofstream@©ofstream©!©write©}§
     4336
     4337void ?{}( ofstream & );§\index{ofstream@©ofstream©!©?{}©}§
    39634338void ?{}( ofstream &, const char name[], const char mode[] = "w" );
    3964 void ^?{}( ofstream & );$\index{ofstream@©ofstream©!©^?{}©}$
     4339void ^?{}( ofstream & );§\index{ofstream@©ofstream©!©^?{}©}§
    39654340
    39664341// *********************************** ifstream ***********************************
    39674342
    3968 bool fail( ifstream & is );$\indexc{fail}\index{ifstream@©ifstream©!©fail©}$
    3969 void clear( ifstream & );$\indexc{clear}\index{ifstream@©ifstream©!©clear©}$
    3970 bool eof( ifstream & is );$\indexc{eof}\index{ifstream@©ifstream©!©eof©}$
    3971 void open( ifstream & is, const char name[], const char mode[] = "r" );$\indexc{open}\index{ifstream@©ifstream©!©open©}$
    3972 void close( ifstream & is );$\indexc{close}\index{ifstream@©ifstream©!©close©}$
    3973 ifstream & read( ifstream & is, char data[], size_t size );$\indexc{read}\index{ifstream@©ifstream©!©read©}$
    3974 ifstream & ungetc( ifstream & is, char c );$\indexc{unget}\index{ifstream@©ifstream©!©unget©}$
    3975 
    3976 void ?{}( ifstream & is );$\index{ifstream@©ifstream©!©?{}©}$
     4343bool fail( ifstream & is );§\indexc{fail}\index{ifstream@©ifstream©!©fail©}§
     4344void clear( ifstream & );§\indexc{clear}\index{ifstream@©ifstream©!©clear©}§
     4345bool eof( ifstream & is );§\indexc{eof}\index{ifstream@©ifstream©!©eof©}§
     4346void open( ifstream & is, const char name[], const char mode[] = "r" );§\indexc{open}\index{ifstream@©ifstream©!©open©}§
     4347void close( ifstream & is );§\indexc{close}\index{ifstream@©ifstream©!©close©}§
     4348ifstream & read( ifstream & is, char data[], size_t size );§\indexc{read}\index{ifstream@©ifstream©!©read©}§
     4349ifstream & ungetc( ifstream & is, char c );§\indexc{unget}\index{ifstream@©ifstream©!©unget©}§
     4350
     4351void ?{}( ifstream & is );§\index{ifstream@©ifstream©!©?{}©}§
    39774352void ?{}( ifstream & is, const char name[], const char mode[] = "r" );
    3978 void ^?{}( ifstream & is );$\index{ifstream@©ifstream©!©^?{}©}$
     4353void ^?{}( ifstream & is );§\index{ifstream@©ifstream©!©^?{}©}§
    39794354\end{cfa}
    39804355\caption{Stream Functions}
     
    40634438The separator string can be at most 16 characters including the ©'\0'© string terminator (15 printable characters).
    40644439\begin{cfa}[belowskip=0pt]
    4065 sepSet( sout, ", $\LstStringStyle{\textdollar}$" ); $\C{// set separator from " " to ", \$"}$
     4440sepSet( sout, ", $\LstStringStyle{\textdollar}$" ); §\C{// set separator from " " to ", \$"}§
    40664441sout | 1 | 2 | 3 | " \"" | ®sepVal® | "\"";
    40674442\end{cfa}
     
    40704445\end{cfa}
    40714446\begin{cfa}[belowskip=0pt]
    4072 sepSet( sout, " " ); $\C{// reset separator to " "}$
     4447sepSet( sout, " " ); §\C{// reset separator to " "}§
    40734448sout | 1 | 2 | 3 | " \"" | ®sepGet( sout )® | "\"";
    40744449\end{cfa}
     
    40784453©sepGet© can be used to store a separator and then restore it:
    40794454\begin{cfa}[belowskip=0pt]
    4080 char store[®sepSize®]; $\C{// sepSize is the maximum separator size}$
    4081 strcpy( store, sepGet( sout ) ); $\C{// copy current separator}$
    4082 sepSet( sout, "_" ); $\C{// change separator to underscore}$
     4455char store[®sepSize®]; §\C{// sepSize is the maximum separator size}§
     4456strcpy( store, sepGet( sout ) ); §\C{// copy current separator}§
     4457sepSet( sout, "_" ); §\C{// change separator to underscore}§
    40834458sout | 1 | 2 | 3;
    40844459\end{cfa}
     
    40874462\end{cfa}
    40884463\begin{cfa}[belowskip=0pt]
    4089 sepSet( sout, store ); $\C{// change separator back to original}$
     4464sepSet( sout, store ); §\C{// change separator back to original}§
    40904465sout | 1 | 2 | 3;
    40914466\end{cfa}
     
    40984473The tuple separator-string can be at most 16 characters including the ©'\0'© string terminator (15 printable characters).
    40994474\begin{cfa}[belowskip=0pt]
    4100 sepSetTuple( sout, " " ); $\C{// set tuple separator from ", " to " "}$
     4475sepSetTuple( sout, " " ); §\C{// set tuple separator from ", " to " "}§
    41014476sout | t1 | t2 | " \"" | ®sepTupleVal® | "\"";
    41024477\end{cfa}
     
    41054480\end{cfa}
    41064481\begin{cfa}[belowskip=0pt]
    4107 sepSetTuple( sout, ", " ); $\C{// reset tuple separator to ", "}$
     4482sepSetTuple( sout, ", " ); §\C{// reset tuple separator to ", "}§
    41084483sout | t1 | t2 | " \"" | ®sepGetTuple( sout )® | "\"";
    41094484\end{cfa}
     
    41164491\Indexc{sepOff}\index{manipulator!sepOff@©sepOff©} and \Indexc{sepOn}\index{manipulator!sepOn@©sepOn©} globally toggle printing the separator.
    41174492\begin{cfa}[belowskip=0pt]
    4118 sout | ®sepOff® | 1 | 2 | 3; $\C{// turn off implicit separator}$
     4493sout | ®sepOff® | 1 | 2 | 3; §\C{// turn off implicit separator}§
    41194494\end{cfa}
    41204495\begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt]
     
    41224497\end{cfa}
    41234498\begin{cfa}[belowskip=0pt]
    4124 sout | ®sepOn® | 1 | 2 | 3; $\C{// turn on implicit separator}$
     4499sout | ®sepOn® | 1 | 2 | 3; §\C{// turn on implicit separator}§
    41254500\end{cfa}
    41264501\begin{cfa}[mathescape=off,showspaces=true,aboveskip=0pt,belowskip=0pt]
     
    41314506\Indexc{sep}\index{manipulator!sep@©sep©} and \Indexc{nosep}\index{manipulator!nosep@©nosep©} locally toggle printing the separator with respect to the next printed item, and then return to the global separator setting.
    41324507\begin{cfa}[belowskip=0pt]
    4133 sout | 1 | ®nosep® | 2 | 3; $\C{// turn off implicit separator for the next item}$
     4508sout | 1 | ®nosep® | 2 | 3; §\C{// turn off implicit separator for the next item}§
    41344509\end{cfa}
    41354510\begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt]
     
    41374512\end{cfa}
    41384513\begin{cfa}[belowskip=0pt]
    4139 sout | sepOff | 1 | ®sep® | 2 | 3; $\C{// turn on implicit separator for the next item}$
     4514sout | sepOff | 1 | ®sep® | 2 | 3; §\C{// turn on implicit separator for the next item}§
    41404515\end{cfa}
    41414516\begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt]
     
    41444519The tuple separator also responses to being turned on and off.
    41454520\begin{cfa}[belowskip=0pt]
    4146 sout | t1 | ®nosep® | t2; $\C{// turn off implicit separator for the next item}$
     4521sout | t1 | ®nosep® | t2; §\C{// turn off implicit separator for the next item}§
    41474522\end{cfa}
    41484523\begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt]
     
    41524527Use ©sep© to accomplish this functionality.
    41534528\begin{cfa}[belowskip=0pt]
    4154 sout | ®sep® | 1 | 2 | 3 | ®sep®; $\C{// sep does nothing at start/end of line}$
     4529sout | ®sep® | 1 | 2 | 3 | ®sep®; §\C{// sep does nothing at start/end of line}§
    41554530\end{cfa}
    41564531\begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt]
     
    41584533\end{cfa}
    41594534\begin{cfa}[belowskip=0pt]
    4160 sout | ®sepVal® | 1 | 2 | 3 | ®sepVal® ; $\C{// use sepVal to print separator at start/end of line}$
     4535sout | ®sepVal® | 1 | 2 | 3 | ®sepVal® ; §\C{// use sepVal to print separator at start/end of line}§
    41614536\end{cfa}
    41624537\begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt]
     
    41974572\Indexc{nl}\index{manipulator!nl@©nl©} inserts a newline.
    41984573\begin{cfa}
    4199 sout | ®nl®; $\C{// only print newline}$
    4200 sout | 2; $\C{// implicit newline}$
    4201 sout | 3 | ®nl® | 4 | ®nl®; $\C{// terminating nl merged with implicit newline}$
    4202 sout | 5 | ®nl® | ®nl®; $\C{// again terminating nl merged with implicit newline}$
    4203 sout | 6; $\C{// implicit newline}$
     4574sout | ®nl®; §\C{// only print newline}§
     4575sout | 2; §\C{// implicit newline}§
     4576sout | 3 | ®nl® | 4 | ®nl®; §\C{// terminating nl merged with implicit newline}§
     4577sout | 5 | ®nl® | ®nl®; §\C{// again terminating nl merged with implicit newline}§
     4578sout | 6; §\C{// implicit newline}§
    42044579
    420545802
     
    46485023®mutex( sout )® {
    46495024        sout | 1;
    4650         ®mutex( sout ) sout® | 2 | 3;                           $\C{// unnecessary, but ok because of recursive lock}$
     5025        ®mutex( sout ) sout® | 2 | 3;                           §\C{// unnecessary, but ok because of recursive lock}§
    46515026        sout | 4;
    46525027} // implicitly release sout lock
     
    46605035        int x, y, z, w;
    46615036        sin | x;
    4662         ®mutex( sin )® sin | y | z;                                     $\C{// unnecessary, but ok because of recursive lock}$
     5037        ®mutex( sin )® sin | y | z;                                     §\C{// unnecessary, but ok because of recursive lock}§
    46635038        sin | w;
    46645039} // implicitly release sin lock
     
    46695044\Textbf{WARNING:} The general problem of \Index{nested locking} can occur if routines are called in an I/O sequence that block, \eg:
    46705045\begin{cfa}
    4671 ®mutex( sout )® sout | "data:" | rtn( mon );    $\C{// mutex call on monitor}$
     5046®mutex( sout )® sout | "data:" | rtn( mon );    §\C{// mutex call on monitor}§
    46725047\end{cfa}
    46735048If the thread executing the I/O expression blocks in the monitor with the ©sout© lock, other threads writing to ©sout© also block until the thread holding the lock is unblocked and releases it.
     
    46875062\begin{cfa}
    4688506312®,®345®.®123          $\C[1.25in]{// comma separator, period decimal-point}$
    4689 12®.®345®,®123          $\C{// period separator, comma decimal-point}$
    4690 12$\Sp$345®,®123®.®     $\C{// space separator, comma decimal-point, period terminator}\CRT$
     506412®.®345®,®123          §\C{// period separator, comma decimal-point}§
     506512$\Sp$345®,®123®.®     §\C{// space separator, comma decimal-point, period terminator}\CRT§
    46915066\end{cfa}
    46925067A locale is selected with function ©setlocale©, and the corresponding locale package \emph{must} be installed on the underlying system;
     
    46995074\begin{cfa}
    47005075#include <fstream.hfa>
    4701 #include <locale.h>                                                     $\C{// setlocale}$
    4702 #include <stdlib.h>                                                     $\C{// getenv}$
     5076#include <locale.h>                                                     §\C{// setlocale}§
     5077#include <stdlib.h>                                                     §\C{// getenv}§
    47035078
    47045079int main() {
     
    47725147int main() {
    47735148        enum { size = 256 };
    4774         char buf[size]; $\C{// output buffer}$
    4775         ®ostrstream osstr = { buf, size };® $\C{// bind output buffer/size}$
     5149        char buf[size]; §\C{// output buffer}§
     5150        ®ostrstream osstr = { buf, size };® §\C{// bind output buffer/size}§
    47765151        int i = 3, j = 5, k = 7;
    47775152        double x = 12345678.9, y = 98765.4321e-11;
    47785153
    47795154        osstr | i | hex(j) | wd(10, k) | sci(x) | unit(eng(y)) | "abc";
    4780         write( osstr ); $\C{// write string to stdout}$
    4781         printf( "%s", buf ); $\C{// same lines of output}$
     5155        write( osstr ); §\C{// write string to stdout}§
     5156        printf( "%s", buf ); §\C{// same lines of output}§
    47825157        sout | i | hex(j) | wd(10, k) | sci(x) | unit(eng(y)) | "abc";
    47835158
    4784         char buf2[] = "12 14 15 3.5 7e4 abc"; $\C{// input buffer}$
     5159        char buf2[] = "12 14 15 3.5 7e4 abc"; §\C{// input buffer}§
    47855160        ®istrstream isstr = { buf2 };®
    47865161        char s[10];
     
    49345309// Subsequent arguments can be specified for initialization
    49355310
    4936 void ?{}( Widget & w ) { $\C{// default constructor}$
     5311void ?{}( Widget & w ) { §\C{// default constructor}§
    49375312        w.id = -1;
    49385313        w.size = 0.0;
     
    49485323
    49495324// ^?{} is the destructor operator identifier
    4950 void ^?{}( Widget & w ) { $\C{// destructor}$
     5325void ^?{}( Widget & w ) { §\C{// destructor}§
    49515326        w.id = 0;
    49525327        w.size = 0.0;
     
    49575332}
    49585333
    4959 Widget baz; $\C{// reserve space only}$
    4960 Widget foo{}; $\C{// calls default constructor}$
    4961 Widget bar{ 23, 2.45 }; $\C{// calls constructor with values}$
    4962 baz{ 24, 0.91 }; $\C{// calls constructor with values}$
    4963 ?{}( baz, 24, 0.91 ); $\C{// explicit call to constructor}$
    4964 ^?{} (bar ); $\C{// explicit call to destructor}$
     5334Widget baz; §\C{// reserve space only}§
     5335Widget foo{}; §\C{// calls default constructor}§
     5336Widget bar{ 23, 2.45 }; §\C{// calls constructor with values}§
     5337baz{ 24, 0.91 }; §\C{// calls constructor with values}§
     5338?{}( baz, 24, 0.91 ); §\C{// explicit call to constructor}§
     5339^?{} (bar ); §\C{// explicit call to destructor}§
    49655340\end{cfa}
    49665341\caption{Constructors and Destructors}
     
    54805855
    54815856®coroutine® Fibonacci {
    5482         int fn; $\C{// used for communication}$
     5857        int fn; §\C{// used for communication}§
    54835858};
    54845859
    5485 void main( Fibonacci & fib ) with( fib ) { $\C{// called on first resume}$
    5486         int fn1, fn2; $\C{// retained between resumes}$
    5487         fn = 0;  fn1 = fn; $\C{// 1st case}$
    5488         ®suspend;® $\C{// restart last resume}$
    5489         fn = 1;  fn2 = fn1;  fn1 = fn; $\C{// 2nd case}$
    5490         ®suspend;® $\C{// restart last resume}$
     5860void main( Fibonacci & fib ) with( fib ) { §\C{// called on first resume}§
     5861        int fn1, fn2; §\C{// retained between resumes}§
     5862        fn = 0;  fn1 = fn; §\C{// 1st case}§
     5863        ®suspend;® §\C{// restart last resume}§
     5864        fn = 1;  fn2 = fn1;  fn1 = fn; §\C{// 2nd case}§
     5865        ®suspend;® §\C{// restart last resume}§
    54915866        for () {
    5492                 fn = fn1 + fn2;  fn2 = fn1;  fn1 = fn; $\C{// general case}$
    5493                 ®suspend;® $\C{// restart last resume}$
     5867                fn = fn1 + fn2;  fn2 = fn1;  fn1 = fn; §\C{// general case}§
     5868                ®suspend;® §\C{// restart last resume}§
    54945869        }
    54955870}
    54965871int next( Fibonacci & fib ) with( fib ) {
    5497         ®resume( fib );® $\C{// restart last suspend}$
     5872        ®resume( fib );® §\C{// restart last suspend}§
    54985873        return fn;
    54995874}
    55005875int main() {
    55015876        Fibonacci f1, f2;
    5502         for ( 10 ) { $\C{// print N Fibonacci values}$
     5877        for ( 10 ) { §\C{// print N Fibonacci values}§
    55035878                sout | next( f1 ) | next( f2 );
    55045879        }
     
    55385913int inc( AtomicCnt & ®mutex® c, int inc = 1 ) with(c) { return counter += inc; }
    55395914int dec( AtomicCnt & ®mutex® c, int dec = 1 ) with(c) { return counter -= dec; }
    5540 forall( ostype & | ostream( ostype ) ) { $\C{// print any stream}$
     5915forall( ostype & | ostream( ostype ) ) { §\C{// print any stream}§
    55415916        ostype & ?|?( ostype & os, AtomicCnt c ) { return os | c.counter; }
    55425917        void ?|?( ostype & os, AtomicCnt c ) { (ostype &)(os | c.counter); ends( os ); }
    55435918}
    55445919
    5545 AtomicCnt global; $\C{// shared}$
     5920AtomicCnt global; §\C{// shared}§
    55465921
    55475922thread MyThread {};
     
    55545929int main() {
    55555930        enum { Threads = 4 };
    5556         processor p[Threads - 1]; $\C{// + starting processor}$
     5931        processor p[Threads - 1]; §\C{// + starting processor}§
    55575932        {
    55585933                MyThread t[Threads];
    55595934        }
    5560         sout | global; $\C{// print 0}$
     5935        sout | global; §\C{// print 0}§
    55615936}
    55625937\end{cfa}
     
    70317406In \CFA, there are ambiguous cases with dereference and operator identifiers, \eg ©int *?*?()©, where the string ©*?*?© can be interpreted as:
    70327407\begin{cfa}
    7033 *?$\Sp$*? $\C{// dereference operator, dereference operator}$
    7034 *$\Sp$?*? $\C{// dereference, multiplication operator}$
     7408*?$\Sp$*? §\C{// dereference operator, dereference operator}§
     7409*$\Sp$?*? §\C{// dereference, multiplication operator}§
    70357410\end{cfa}
    70367411By default, the first interpretation is selected, which does not yield a meaningful parse.
     
    70847459\eg:
    70857460\begin{cfa}
    7086 x; $\C{// int x}$
    7087 *y; $\C{// int *y}$
    7088 f( p1, p2 ); $\C{// int f( int p1, int p2 );}$
    7089 g( p1, p2 ) int p1, p2; $\C{// int g( int p1, int p2 );}$
     7461x; §\C{// int x}§
     7462*y; §\C{// int *y}§
     7463f( p1, p2 ); §\C{// int f( int p1, int p2 );}§
     7464g( p1, p2 ) int p1, p2; §\C{// int g( int p1, int p2 );}§
    70907465\end{cfa}
    70917466\CFA continues to support K\&R routine definitions:
    70927467\begin{cfa}
    7093 f( a, b, c ) $\C{// default int return}$
    7094         int a, b; char c $\C{// K\&R parameter declarations}$
     7468f( a, b, c ) §\C{// default int return}§
     7469        int a, b; char c §\C{// K\&R parameter declarations}§
    70957470{
    70967471        ...
     
    71117486int rtn( int i );
    71127487int rtn( char c );
    7113 rtn( 'x' ); $\C{// programmer expects 2nd rtn to be called}$
     7488rtn( 'x' ); §\C{// programmer expects 2nd rtn to be called}§
    71147489\end{cfa}
    71157490\item[Rationale:] it is more intuitive for the call to ©rtn© to match the second version of definition of ©rtn© rather than the first.
     
    71337508\item[Change:] make string literals ©const©:
    71347509\begin{cfa}
    7135 char * p = "abc"; $\C{// valid in C, deprecated in \CFA}$
    7136 char * q = expr ? "abc" : "de"; $\C{// valid in C, invalid in \CFA}$
     7510char * p = "abc"; §\C{// valid in C, deprecated in \CFA}§
     7511char * q = expr ? "abc" : "de"; §\C{// valid in C, invalid in \CFA}§
    71377512\end{cfa}
    71387513The type of a string literal is changed from ©[] char© to ©const [] char©.
     
    71417516\begin{cfa}
    71427517char * p = "abc";
    7143 p[0] = 'w'; $\C{// segment fault or change constant literal}$
     7518p[0] = 'w'; §\C{// segment fault or change constant literal}§
    71447519\end{cfa}
    71457520The same problem occurs when passing a string literal to a routine that changes its argument.
     
    71537528\item[Change:] remove \newterm{tentative definitions}, which only occurs at file scope:
    71547529\begin{cfa}
    7155 int i; $\C{// forward definition}$
    7156 int *j = ®&i®; $\C{// forward reference, valid in C, invalid in \CFA}$
    7157 int i = 0; $\C{// definition}$
     7530int i; §\C{// forward definition}§
     7531int *j = ®&i®; §\C{// forward reference, valid in C, invalid in \CFA}§
     7532int i = 0; §\C{// definition}§
    71587533\end{cfa}
    71597534is valid in C, and invalid in \CFA because duplicate overloaded object definitions at the same scope level are disallowed.
     
    71617536\begin{cfa}
    71627537struct X { int i; struct X *next; };
    7163 static struct X a; $\C{// forward definition}$
    7164 static struct X b = { 0, ®&a® };$\C{// forward reference, valid in C, invalid in \CFA}$
    7165 static struct X a = { 1, &b }; $\C{// definition}$
     7538static struct X a; §\C{// forward definition}§
     7539static struct X b = { 0, ®&a® };§\C{// forward reference, valid in C, invalid in \CFA}§
     7540static struct X a = { 1, &b }; §\C{// definition}§
    71667541\end{cfa}
    71677542\item[Rationale:] avoids having different initialization rules for builtin types and user-defined types.
     
    71787553struct Person {
    71797554        enum ®Colour® { R, G, B };      $\C[7cm]{// nested type}$
    7180         struct Face { $\C{// nested type}$
    7181                 ®Colour® Eyes, Hair; $\C{// type defined outside (1 level)}$
     7555        struct Face { §\C{// nested type}§
     7556                ®Colour® Eyes, Hair; §\C{// type defined outside (1 level)}§
    71827557        };
    7183         ®.Colour® shirt; $\C{// type defined outside (top level)}$
    7184         ®Colour® pants; $\C{// type defined same level}$
    7185         Face looks[10]; $\C{// type defined same level}$
     7558        ®.Colour® shirt; §\C{// type defined outside (top level)}§
     7559        ®Colour® pants; §\C{// type defined same level}§
     7560        Face looks[10]; §\C{// type defined same level}§
    71867561};
    7187 ®Colour® c = R; $\C{// type/enum defined same level}$
    7188 Person®.Colour® pc = Person®.®R;$\C{// type/enum defined inside}$
    7189 Person®.®Face pretty; $\C{// type defined inside}\CRT$
     7562®Colour® c = R; §\C{// type/enum defined same level}§
     7563Person®.Colour® pc = Person®.®R;§\C{// type/enum defined inside}§
     7564Person®.®Face pretty; §\C{// type defined inside}\CRT§
    71907565\end{cfa}
    71917566In C, the name of the nested types belongs to the same scope as the name of the outermost enclosing structure, \ie the nested types are hoisted to the scope of the outer-most type, which is not useful and confusing.
     
    72047579\item[Difficulty of converting:] Semantic transformation. To make the struct type name visible in the scope of the enclosing struct, the struct tag could be declared in the scope of the enclosing struct, before the enclosing struct is defined. Example:
    72057580\begin{cfa}
    7206 struct Y; $\C{// struct Y and struct X are at the same scope}$
     7581struct Y; §\C{// struct Y and struct X are at the same scope}§
    72077582struct X {
    72087583        struct Y { /* ... */ } y;
     
    72197594\begin{cfa}
    72207595void foo() {
    7221         int * b = malloc( sizeof(int) ); $\C{// implicitly convert void * to int *}$
    7222         char * c = b; $\C{// implicitly convert int * to void *, and then void * to char *}$
     7596        int * b = malloc( sizeof(int) ); §\C{// implicitly convert void * to int *}§
     7597        char * c = b; §\C{// implicitly convert int * to void *, and then void * to char *}§
    72237598}
    72247599\end{cfa}
     
    74627837Type-safe allocation is provided for all C allocation routines and new \CFA allocation routines, \eg in
    74637838\begin{cfa}
    7464 int * ip = (int *)malloc( sizeof(int) );                $\C{// C}$
    7465 int * ip = malloc();                                                    $\C{// \CFA type-safe version of C malloc}$
    7466 int * ip = alloc();                                                             $\C{// \CFA type-safe uniform alloc}$
     7839int * ip = (int *)malloc( sizeof(int) );                §\C{// C}§
     7840int * ip = malloc();                                                    §\C{// \CFA type-safe version of C malloc}§
     7841int * ip = alloc();                                                             §\C{// \CFA type-safe uniform alloc}§
    74677842\end{cfa}
    74687843the latter two allocations determine the allocation size from the type of ©p© (©int©) and cast the pointer to the allocated storage to ©int *©.
     
    74717846\begin{cfa}
    74727847struct S { int i; } __attribute__(( aligned( 128 ) )); // cache-line alignment
    7473 S * sp = malloc();                                                              $\C{// honour type alignment}$
     7848S * sp = malloc();                                                              §\C{// honour type alignment}§
    74747849\end{cfa}
    74757850the storage allocation is implicitly aligned to 128 rather than the default 16.
     
    74867861\CFA memory management extends allocation to support constructors for initialization of allocated storage, \eg in
    74877862\begin{cfa}
    7488 struct S { int i; };                                                    $\C{// cache-line alignment}$
     7863struct S { int i; };                                                    §\C{// cache-line alignment}§
    74897864void ?{}( S & s, int i ) { s.i = i; }
    74907865// assume ?|? operator for printing an S
    74917866
    7492 S & sp = *®new®( 3 );                                                   $\C{// call constructor after allocation}$
     7867S & sp = *®new®( 3 );                                                   §\C{// call constructor after allocation}§
    74937868sout | sp.i;
    74947869®delete®( &sp );
    74957870
    7496 S * spa = ®anew®( 10, 5 );                                              $\C{// allocate array and initialize each array element}$
     7871S * spa = ®anew®( 10, 5 );                                              §\C{// allocate array and initialize each array element}§
    74977872for ( i; 10 ) sout | spa[i] | nonl;
    74987873sout | nl;
     
    75337908        // $\CFA$ safe general allocation, fill, resize, alignment, array
    75347909        T * alloc( void );$\indexc{alloc}$                                      $\C[3.5in]{// variable, T size}$
    7535         T * alloc( size_t dim );                                                        $\C{// array[dim], T size elements}$
    7536         T * alloc( T ptr[], size_t dim );                                       $\C{// realloc array[dim], T size elements}$
    7537 
    7538         T * alloc_set( char fill );$\indexc{alloc_set}$         $\C{// variable, T size, fill bytes with value}$
    7539         T * alloc_set( T fill );                                                        $\C{// variable, T size, fill with value}$
    7540         T * alloc_set( size_t dim, char fill );                         $\C{// array[dim], T size elements, fill bytes with value}$
    7541         T * alloc_set( size_t dim, T fill );                            $\C{// array[dim], T size elements, fill elements with value}$
    7542         T * alloc_set( size_t dim, const T fill[] );            $\C{// array[dim], T size elements, fill elements with array}$
    7543         T * alloc_set( T ptr[], size_t dim, char fill );        $\C{// realloc array[dim], T size elements, fill bytes with value}$
    7544 
    7545         T * alloc_align( size_t align );                                        $\C{// aligned variable, T size}$
    7546         T * alloc_align( size_t align, size_t dim );            $\C{// aligned array[dim], T size elements}$
    7547         T * alloc_align( T ptr[], size_t align );                       $\C{// realloc new aligned array}$
    7548         T * alloc_align( T ptr[], size_t align, size_t dim ); $\C{// realloc new aligned array[dim]}$
    7549 
    7550         T * alloc_align_set( size_t align, char fill );         $\C{// aligned variable, T size, fill bytes with value}$
    7551         T * alloc_align_set( size_t align, T fill );            $\C{// aligned variable, T size, fill with value}$
    7552         T * alloc_align_set( size_t align, size_t dim, char fill ); $\C{// aligned array[dim], T size elements, fill bytes with value}$
    7553         T * alloc_align_set( size_t align, size_t dim, T fill ); $\C{// aligned array[dim], T size elements, fill elements with value}$
    7554         T * alloc_align_set( size_t align, size_t dim, const T fill[] ); $\C{// aligned array[dim], T size elements, fill elements with array}$
    7555         T * alloc_align_set( T ptr[], size_t align, size_t dim, char fill ); $\C{// realloc new aligned array[dim], fill new bytes with value}$
     7910        T * alloc( size_t dim );                                                        §\C{// array[dim], T size elements}§
     7911        T * alloc( T ptr[], size_t dim );                                       §\C{// realloc array[dim], T size elements}§
     7912
     7913        T * alloc_set( char fill );$\indexc{alloc_set}$         §\C{// variable, T size, fill bytes with value}§
     7914        T * alloc_set( T fill );                                                        §\C{// variable, T size, fill with value}§
     7915        T * alloc_set( size_t dim, char fill );                         §\C{// array[dim], T size elements, fill bytes with value}§
     7916        T * alloc_set( size_t dim, T fill );                            §\C{// array[dim], T size elements, fill elements with value}§
     7917        T * alloc_set( size_t dim, const T fill[] );            §\C{// array[dim], T size elements, fill elements with array}§
     7918        T * alloc_set( T ptr[], size_t dim, char fill );        §\C{// realloc array[dim], T size elements, fill bytes with value}§
     7919
     7920        T * alloc_align( size_t align );                                        §\C{// aligned variable, T size}§
     7921        T * alloc_align( size_t align, size_t dim );            §\C{// aligned array[dim], T size elements}§
     7922        T * alloc_align( T ptr[], size_t align );                       §\C{// realloc new aligned array}§
     7923        T * alloc_align( T ptr[], size_t align, size_t dim ); §\C{// realloc new aligned array[dim]}§
     7924
     7925        T * alloc_align_set( size_t align, char fill );         §\C{// aligned variable, T size, fill bytes with value}§
     7926        T * alloc_align_set( size_t align, T fill );            §\C{// aligned variable, T size, fill with value}§
     7927        T * alloc_align_set( size_t align, size_t dim, char fill ); §\C{// aligned array[dim], T size elements, fill bytes with value}§
     7928        T * alloc_align_set( size_t align, size_t dim, T fill ); §\C{// aligned array[dim], T size elements, fill elements with value}§
     7929        T * alloc_align_set( size_t align, size_t dim, const T fill[] ); §\C{// aligned array[dim], T size elements, fill elements with array}§
     7930        T * alloc_align_set( T ptr[], size_t align, size_t dim, char fill ); §\C{// realloc new aligned array[dim], fill new bytes with value}§
    75567931
    75577932        // $\CFA$ safe initialization/copy, i.e., implicit size specification
     
    76147989\leavevmode
    76157990\begin{cfa}[aboveskip=0pt,belowskip=0pt]
    7616 forall( T | { int ?<?( T, T ); } ) $\C{// location}$
     7991forall( T | { int ?<?( T, T ); } ) §\C{// location}§
    76177992T * bsearch( T key, const T * arr, size_t dim );$\indexc{bsearch}$
    76187993
    7619 forall( T | { int ?<?( T, T ); } ) $\C{// position}$
     7994forall( T | { int ?<?( T, T ); } ) §\C{// position}§
    76207995unsigned int bsearch( T key, const T * arr, size_t dim );
    76217996
     
    76247999
    76258000forall( E | { int ?<?( E, E ); } ) {
    7626         E * bsearch( E key, const E * vals, size_t dim );$\indexc{bsearch}$ $\C{// location}$
    7627         size_t bsearch( E key, const E * vals, size_t dim );$\C{// position}$
     8001        E * bsearch( E key, const E * vals, size_t dim );$\indexc{bsearch}$ §\C{// location}§
     8002        size_t bsearch( E key, const E * vals, size_t dim );§\C{// position}§
    76288003        E * bsearchl( E key, const E * vals, size_t dim );$\indexc{bsearchl}$
    76298004        size_t bsearchl( E key, const E * vals, size_t dim );
     
    76728047void srandom( unsigned int seed );$\indexc{srandom}$
    76738048char random( void );$\indexc{random}$
    7674 char random( char u ); $\C{// [0,u)}$
    7675 char random( char l, char u ); $\C{// [l,u]}$
     8049char random( char u ); §\C{// [0,u)}§
     8050char random( char l, char u ); §\C{// [l,u]}§
    76768051int random( void );
    7677 int random( int u ); $\C{// [0,u)}$
    7678 int random( int l, int u ); $\C{// [l,u]}$
     8052int random( int u ); §\C{// [0,u)}§
     8053int random( int l, int u ); §\C{// [l,u]}§
    76798054unsigned int random( void );
    7680 unsigned int random( unsigned int u ); $\C{// [0,u)}$
    7681 unsigned int random( unsigned int l, unsigned int u ); $\C{// [l,u]}$
     8055unsigned int random( unsigned int u ); §\C{// [0,u)}§
     8056unsigned int random( unsigned int l, unsigned int u ); §\C{// [l,u]}§
    76828057long int random( void );
    7683 long int random( long int u ); $\C{// [0,u)}$
    7684 long int random( long int l, long int u ); $\C{// [l,u]}$
     8058long int random( long int u ); §\C{// [0,u)}§
     8059long int random( long int l, long int u ); §\C{// [l,u]}§
    76858060unsigned long int random( void );
    7686 unsigned long int random( unsigned long int u ); $\C{// [0,u)}$
    7687 unsigned long int random( unsigned long int l, unsigned long int u ); $\C{// [l,u]}$
    7688 float random( void );                                            $\C{// [0.0, 1.0)}$
    7689 double random( void );                                           $\C{// [0.0, 1.0)}$
    7690 float _Complex random( void );                           $\C{// [0.0, 1.0)+[0.0, 1.0)i}$
    7691 double _Complex random( void );                          $\C{// [0.0, 1.0)+[0.0, 1.0)i}$
    7692 long double _Complex random( void );             $\C{// [0.0, 1.0)+[0.0, 1.0)i}$
     8061unsigned long int random( unsigned long int u ); §\C{// [0,u)}§
     8062unsigned long int random( unsigned long int l, unsigned long int u ); §\C{// [l,u]}§
     8063float random( void );                                            §\C{// [0.0, 1.0)}§
     8064double random( void );                                           §\C{// [0.0, 1.0)}§
     8065float _Complex random( void );                           §\C{// [0.0, 1.0)+[0.0, 1.0)i}§
     8066double _Complex random( void );                          §\C{// [0.0, 1.0)+[0.0, 1.0)i}§
     8067long double _Complex random( void );             §\C{// [0.0, 1.0)+[0.0, 1.0)i}§
    76938068\end{cfa}
    76948069
     
    78898264long double atan2( long double, long double );
    78908265
    7891 float atan( float, float ); $\C{// alternative name for atan2}$
     8266float atan( float, float ); §\C{// alternative name for atan2}§
    78928267double atan( double, double );$\indexc{atan}$
    78938268long double atan( long double, long double );
     
    81168491\begin{cfa}[aboveskip=0pt,belowskip=0pt]
    81178492struct Duration {
    8118         int64_t tn; $\C{// nanoseconds}$
     8493        int64_t tn; §\C{// nanoseconds}§
    81198494};
    81208495
     
    82618636\begin{cfa}[aboveskip=0pt,belowskip=0pt]
    82628637struct Time {
    8263         uint64_t tn; $\C{// nanoseconds since UNIX epoch}$
     8638        uint64_t tn; §\C{// nanoseconds since UNIX epoch}§
    82648639};
    82658640
     
    83268701\leavevmode
    83278702\begin{cfa}[aboveskip=0pt,belowskip=0pt]
    8328 struct Clock { $\C{// virtual clock}$
    8329         Duration offset; $\C{// offset from computer real-time}$
     8703struct Clock { §\C{// virtual clock}§
     8704        Duration offset; §\C{// offset from computer real-time}§
    83308705};
    83318706
    8332 void ?{}( Clock & clk ); $\C{// create no offset}$
    8333 void ?{}( Clock & clk, Duration adj ); $\C{// create with offset}$
    8334 void reset( Clock & clk, Duration adj ); $\C{// change offset}$
    8335 
    8336 Duration resolutionHi(); $\C{// clock resolution in nanoseconds (fine)}$
    8337 Duration resolution(); $\C{// clock resolution without nanoseconds (coarse)}$
    8338 
    8339 Time timeHiRes(); $\C{// real time with nanoseconds}$
    8340 Time time(); $\C{// real time without nanoseconds}$
    8341 Time time( Clock & clk ); $\C{// real time for given clock}$
    8342 Time ?()( Clock & clk ); $\C{//\ \ \ \ alternative syntax}$
    8343 timeval time( Clock & clk ); $\C{// convert to C time format}$
     8707void ?{}( Clock & clk ); §\C{// create no offset}§
     8708void ?{}( Clock & clk, Duration adj ); §\C{// create with offset}§
     8709void reset( Clock & clk, Duration adj ); §\C{// change offset}§
     8710
     8711Duration resolutionHi(); §\C{// clock resolution in nanoseconds (fine)}§
     8712Duration resolution(); §\C{// clock resolution without nanoseconds (coarse)}§
     8713
     8714Time timeHiRes(); §\C{// real time with nanoseconds}§
     8715Time time(); §\C{// real time without nanoseconds}§
     8716Time time( Clock & clk ); §\C{// real time for given clock}§
     8717Time ?()( Clock & clk ); §\C{//\ \ \ \ alternative syntax}§
     8718timeval time( Clock & clk ); §\C{// convert to C time format}§
    83448719tm time( Clock & clk );
    8345 Duration processor(); $\C{// non-monotonic duration of kernel thread}$
    8346 Duration program(); $\C{// non-monotonic duration of program CPU}$
    8347 Duration boot(); $\C{// monotonic duration since computer boot}$
     8720Duration processor(); §\C{// non-monotonic duration of kernel thread}§
     8721Duration program(); §\C{// non-monotonic duration of program CPU}§
     8722Duration boot(); §\C{// monotonic duration since computer boot}§
    83488723\end{cfa}
    83498724
     
    83868761\begin{cfa}
    83878762struct PRNG { ... }; $\C[3.75in]{// opaque type}$
    8388 void ?{}( PRNG & prng ); $\C{// random seed}$
    8389 void ?{}( PRNG & prng, uint32_t seed ); $\C{// fixed seed}$
    8390 void set_seed( PRNG & prng, uint32_t seed ); $\C{// set seed}$
    8391 uint32_t get_seed( PRNG & prng ); $\C{// get seed}$
    8392 uint32_t prng( PRNG & prng ); $\C{// [0,UINT\_MAX]}$
    8393 uint32_t prng( PRNG & prng, uint32_t u ); $\C{// [0,u)}$
    8394 uint32_t prng( PRNG & prng, uint32_t l, uint32_t u ); $\C{// [l,u]}$
    8395 uint32_t calls( PRNG & prng ); $\C{// number of calls}\CRT$
     8763void ?{}( PRNG & prng ); §\C{// random seed}§
     8764void ?{}( PRNG & prng, uint32_t seed ); §\C{// fixed seed}§
     8765void set_seed( PRNG & prng, uint32_t seed ); §\C{// set seed}§
     8766uint32_t get_seed( PRNG & prng ); §\C{// get seed}§
     8767uint32_t prng( PRNG & prng ); §\C{// [0,UINT\_MAX]}§
     8768uint32_t prng( PRNG & prng, uint32_t u ); §\C{// [0,u)}§
     8769uint32_t prng( PRNG & prng, uint32_t l, uint32_t u ); §\C{// [l,u]}§
     8770uint32_t calls( PRNG & prng ); §\C{// number of calls}\CRT§
    83968771\end{cfa}
    83978772A ©PRNG© object is used to randomize behaviour or values during execution, \eg in games, a character makes a random move or an object takes on a random value.
     
    84478822\begin{cfa}
    84488823void set_seed( uint32_t seed ); $\C[3.75in]{// set global seed}$
    8449 uint32_t get_seed(); $\C{// get global seed}$
     8824uint32_t get_seed(); §\C{// get global seed}§
    84508825// SLOWER
    8451 uint32_t prng(); $\C{// [0,UINT\_MAX]}$
    8452 uint32_t prng( uint32_t u ); $\C{// [0,u)}$
    8453 uint32_t prng( uint32_t l, uint32_t u ); $\C{// [l,u]}$
     8826uint32_t prng(); §\C{// [0,UINT\_MAX]}§
     8827uint32_t prng( uint32_t u ); §\C{// [0,u)}§
     8828uint32_t prng( uint32_t l, uint32_t u ); §\C{// [l,u]}§
    84548829// FASTER
    8455 uint32_t prng( $thread\LstStringStyle{\textdollar}$ & th );     $\C{// [0,UINT\_MAX]}$
    8456 uint32_t prng( $thread\LstStringStyle{\textdollar}$ & th, uint32_t u ); $\C{// [0,u)}$
    8457 uint32_t prng( $thread\LstStringStyle{\textdollar}$ & th, uint32_t l, uint32_t u );     $\C{// [l,u]}\CRT$
     8830uint32_t prng( $thread\LstStringStyle{\textdollar}$ & th );     §\C{// [0,UINT\_MAX]}§
     8831uint32_t prng( $thread\LstStringStyle{\textdollar}$ & th, uint32_t u ); §\C{// [0,u)}§
     8832uint32_t prng( $thread\LstStringStyle{\textdollar}$ & th, uint32_t l, uint32_t u );     §\C{// [l,u]}\CRT§
    84588833\end{cfa}
    84598834The only difference between the two sets of ©prng© routines is performance.
     
    85368911
    85378912\begin{cfa}
    8538 void ?{}( Int * this ); $\C{// constructor/destructor}$
     8913void ?{}( Int * this ); §\C{// constructor/destructor}§
    85398914void ?{}( Int * this, Int init );
    85408915void ?{}( Int * this, zero_t );
     
    85458920void ^?{}( Int * this );
    85468921
    8547 Int ?=?( Int * lhs, Int rhs ); $\C{// assignment}$
     8922Int ?=?( Int * lhs, Int rhs ); §\C{// assignment}§
    85488923Int ?=?( Int * lhs, long int rhs );
    85498924Int ?=?( Int * lhs, unsigned long int rhs );
     
    85628937unsigned long int narrow( Int val );
    85638938
    8564 int ?==?( Int oper1, Int oper2 ); $\C{// comparison}$
     8939int ?==?( Int oper1, Int oper2 ); §\C{// comparison}§
    85658940int ?==?( Int oper1, long int oper2 );
    85668941int ?==?( long int oper2, Int oper1 );
     
    85988973int ?>=?( unsigned long int oper1, Int oper2 );
    85998974
    8600 Int +?( Int oper ); $\C{// arithmetic}$
     8975Int +?( Int oper ); §\C{// arithmetic}§
    86018976Int -?( Int oper );
    86028977Int ~?( Int oper );
     
    86809055Int ?>>=?( Int * lhs, mp_bitcnt_t shift );
    86819056
    8682 Int abs( Int oper ); $\C{// number functions}$
     9057Int abs( Int oper ); §\C{// number functions}§
    86839058Int fact( unsigned long int N );
    86849059Int gcd( Int oper1, Int oper2 );
     
    86929067Int sqrt( Int oper );
    86939068
    8694 forall( dtype istype | istream( istype ) ) istype * ?|?( istype * is, Int * mp );  $\C{// I/O}$
     9069forall( dtype istype | istream( istype ) ) istype * ?|?( istype * is, Int * mp );  §\C{// I/O}§
    86959070forall( dtype ostype | ostream( ostype ) ) ostype * ?|?( ostype * os, Int mp );
    86969071\end{cfa}
     
    87919166// implementation
    87929167struct Rational {$\indexc{Rational}$
    8793         long int numerator, denominator; $\C{// invariant: denominator > 0}$
     9168        long int numerator, denominator; §\C{// invariant: denominator > 0}§
    87949169}; // Rational
    87959170
    8796 Rational rational(); $\C{// constructors}$
     9171Rational rational(); §\C{// constructors}§
    87979172Rational rational( long int n );
    87989173Rational rational( long int n, long int d );
     
    88009175void ?{}( Rational * r, one_t );
    88019176
    8802 long int numerator( Rational r ); $\C{// numerator/denominator getter/setter}$
     9177long int numerator( Rational r ); §\C{// numerator/denominator getter/setter}§
    88039178long int numerator( Rational r, long int n );
    88049179long int denominator( Rational r );
    88059180long int denominator( Rational r, long int d );
    88069181
    8807 int ?==?( Rational l, Rational r ); $\C{// comparison}$
     9182int ?==?( Rational l, Rational r ); §\C{// comparison}§
    88089183int ?!=?( Rational l, Rational r );
    88099184int ?<?( Rational l, Rational r );
     
    88129187int ?>=?( Rational l, Rational r );
    88139188
    8814 Rational -?( Rational r ); $\C{// arithmetic}$
     9189Rational -?( Rational r ); §\C{// arithmetic}§
    88159190Rational ?+?( Rational l, Rational r );
    88169191Rational ?-?( Rational l, Rational r );
     
    88189193Rational ?/?( Rational l, Rational r );
    88199194
    8820 double widen( Rational r ); $\C{// conversion}$
     9195double widen( Rational r ); §\C{// conversion}§
    88219196Rational narrow( double f, long int md );
    88229197
Note: See TracChangeset for help on using the changeset viewer.