Docstoc

STL Standard Template Library

Document Sample
STL Standard Template Library Powered By Docstoc
					Functions
    Declarations and Definitions
    Function Declarations
    Function Arguments
    Function Return Types
    Overloaded Functions
    Function Templates
Declarations and Definitions
File A.C                       Trans. Unit A.C
#include "A.h"                 extern int day_of_year;
int day_of_year=31;            extern double f(double);
double f(double x) {           int day_of_year=31;
    return 1.0/x;              double f(double x) {
}                                  return 1.0/x;
                               }
File A.h
extern int day_of_year;
extern double f(double);
                               Trans. Unit useA.C
                               extern int day_of_year;
File useA.C                    extern double f(double);
#include "A.h"                 ...
                               day_of_year++;
day_of_year++;                 double one_third = f(3);
double one_third = f(3);       ...
Function Declarations
linkage(optional) identifier type(return type, argument types)

extern double sin(double x);
extern complex sin(complex x);
extern float sin(double x); //Compiler Error

static double exp_random(double mu) { ... }
inline double sqr(double x) {return x*x;}




When declaring a name with external linkage, omit the
extern keyword in definitions and use the extern
keyword in declarations.
Function Arguments
    Anything that occurs during the execution of a
     function other than the mapping of the input to
     the output is called side-effect.
Pure side-effects are best provided in the context of
classes.
Avoid pure side-effects in global functions.
   float s(float a, float b) {
       a = b;
       return a*b;
   }

   float x = 12.1;
   s(x,4);
                                          conversion
            float    int
float x :
             12.1     4


            float    int               float               float
float x :                  float a :           float b :
             12.1     4                 12.1                4.0

            float    int               float               float
float x :                  float a :           float b :
             12.1     4                 4.0                 4.0
float s(float& a, float b) {
    a = b;
    return a*b;
}

float x = 12.1;
s(x,4);


             float   int
float x :                      conversion
              12.1    4

float x :
          float      int                        float
                                    float b :
float a : 12.1        4                          4.0


float x :
          float      int                        float
                                    float b :
float a : 4.0         4                          4.0
float s(float& a, const float& b) {
    a = b;
    return a*b;
}

float x = 12.1;
s(x,4);


             float   int
float x :                              conversion
              12.1    4

float x :
          float      int                              float
float a : 12.1        4               const float b : 4.0


float x :
          float      int                              float
float a : 4.0         4               const float b : 4.0
Signal input-modification side-effects by consistent
use of type modifiers in the declaration of the forma l
arguments.

Assume that an argument passed by (non-const)
reference is modified.

Pass small objects by value and large objects by
const reference, if the arguments will not be
modified.

Declare C++ built-in array arguments const, unless
the function is intended to alter the array elements.
extern double g(const float y[]);
extern double g(const float* y);
#include <math.h>
extern double log_of(double x, double base = M_E); // M_E in math.h

log_of(2.);
log_of(2.,10.);

extern double f(int x = 3, double y); // Compile Error

extern void doNothing();




Supply default arguments with the function's declaration
in the header file, not with the function's definition in
the implementation file.
extern void display_greeting();

float f() {
    ...
    return 1;
}
                                   Use return by reference to
float& second_element(float* a){   provide access to a selected
    return a[1];
}                                  portion of an object or of a
float x[] = {1, 1};                function argument passed
second_element(x) = 10;
cout << x[1] << endl;
                                   by reference.
double& g() {
    double x;
    ...
                   Do not return (non-static) local variables
    return x;      or constants by reference.
}
Overloaded Functions
    Exact Match or Match with Trivial Conversions

extern int abs(int);
extern double abs(double);
extern double abs(complex);

int i = -3;
int iabs    = abs(i);   // Calls abs(int)
double d = 3.2;
double dabs = abs(d);   // Calls abs(double)
complex c(-1, 2);
double cabs = abs(c);   // Calls abs(complex)

extern int abs(int&);
extern double abs(double&);
extern double abs(complex&);
     Match with Promotions

 extern double exp(int y);
 extern double exp(double y);


     Match with Standard Conversions
 extern double exp(double y);


     Match with User-Defined Conversions

Used simply, function overloading avoids needless
repetition of function definitions for similar types of
arguments. But overused or used in a way that depends
on subtleties of the C++ rules, overloading is confusing.
    Function Templates
template<class EltType>
extern void axpy(EltType alpha, const Vector<EltType>& x,
                 Vector<EltType>& y);

void axpy(float   alpha, const Vector<float>&   x, Vector<float>&   y);
void axpy(double alpha, const Vector<double>& x, Vector<double>& y);
void axpy(complex alpha, const Vector<complex>& x, Vector<complex>& y);

void axpy(float      alpha, const Vector<double>&        x, Vector<double>&     y);


 If an exact match of a nontemplate function is found, the matching function is
  called.
 Otherwise, if a function template that matches (with only trivial conversions) is
  found, it is used.
 Otherwise, if a function is found using ordinary argument matching process, it is
  used.

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:3
posted:8/11/2012
language:English
pages:13