Typedef and Preprocessor directives
typedef
#include <iostream>
#include <vector>
typedef std::vector<int> intContainer;
int main(int argc, char *argv[])
{
intContainer c;
for(int i = 0; i < 10; i++)
c.push_back(i);
for(int i = 0; i < 10; i++)
std::cout<<c[i]<<" ";
std::cout<<std::endl;
return 0;
}
//g++ main.cpp -std=c++11
#include <iostream>
#include <vector>
using intContainer = std::vector<int>;
int main(int argc, char *argv[])
{
intContainer c;
for(int i = 0; i < 10; i++)
c.push_back(i);
for(int i = 0; i < 10; i++)
std::cout<<c[i]<<" ";
std::cout<<std::endl;
return 0;
}
#include
- #include <filename>, look for the file in a special place defined by the operating system
- #include "filename", search the self-defined file in a specified directory
#define and #undef
#include <iostream>
#define SIZE 10
#define getMax(a, b) ((a>b)?a:b)
int main(int argc, char *argv[])
{
std::cout<<SIZE<<std::endl;
std::cout<<getMax(1, 2)<<std::endl;
return 0;
}
#define str(x) #x
#define glue(a, b) a ## b
#include <iostream>
int main(int argc, char *argv[])
{
//std::cout<<"Hello World"<<std::endl;
std::cout<<str(Hello World)<<std::endl;
using std::cout;
glue(c, out)<<"Hello World"<<std::endl;
return 0;
}
- Directive are valid from the point of definition to the end of the file in which they are defined
- Defined macros lasts until it is undefined with the #undef preprocessor directive
- #, replace parameter name to a string
- ##, concatenates two arguments leaving no blank spaces between them
#ifdef, #ifndef, and #endif
#define PRINT_JOE
#include <iostream>
int main(int argc, char *argv[])
{
#ifdef PRINT_JOE
std::cout<<"JOE ..."<<std::endl;
#endif
#ifndef PRINT_BOB
std::cout<<"BOB ..."<<std::endl;
#endif
return 0;
}
#if, #else, and #elif
#define SIZE 150
#include <iostream>
int main(int argc, char *argv[])
{
#if SIZE < 100
#undef SIZE
#define SIZE 100
#elif SIZE < 200
#undef SIZE
#define SIZE 200
#else
#undef SIZE
#define SIZE 300
#endif
std::cout<<SIZE<<std::endl;
return 0;
}
#line
#include <iostream>
int main(int argc, char *argv[])
{
#line 100
#line 100 "temp.cpp"
std::cout<<__FILE__<<" "<<__LINE__<<std::endl;
return 0;
}
Define the file name and the starting line number, changes the results of __FILE__ and __LINE__
#error
#ifndef __cplusplus
#error A C++ compiler is required!
#endif
#include <iostream>
int main(int argc, char *argv[])
{
return 0;
}
Aborts the compliation process if the mmacro name __cplusplus is not defined
#pragma
Specify diverse options to the compiler for the platform and the compiler used, such as in OpenACC and OpenMP
Predefined Macros
#include <iostream>
int main(int argc, char *argv[])
{
//__LINE__
std::cout<<__LINE__<<std::endl;
//__FILE__
std::cout<<__FILE__<<std::endl;
//__DATE__
std::cout<<__DATE__<<std::endl;
//__TIME__
std::cout<<__TIME__<<std::endl;
//__STDC__, signify the compiler conforms to ISO Standard C
std::cout<<__STDC__<<std::endl;
//__STDC_HOSTED__, 1 if the implementation is a hosted implementation
std::cout<<__STDC_HOSTED__<<std::endl;
//__cplusplus, defined when the C++ compiler is in user
std::cout<<__cplusplus<<std::endl;
//__STDCPP_THREADS__, 1 if the program have more than one thread
//std::cout<<__STDCPP_THREADS__<<std::endl;
return 0;
}
#include <iostream>
#include <typeinfo>
int main(int argc, char *argv[])
{
//__GNUG__, defined if GNU C++ Compiler is in use
std::cout<<__GNUG__<<std::endl;
//__BASE_FILE__
std::cout<<__BASE_FILE__<<std::endl;
//__INCLUDE_LEVEL__
std::cout<<__INCLUDE_LEVEL__<<std::endl;
//__VERSION__, compiler version
std::cout<<__VERSION__<<std::endl;
//__INT8_TYPE__, char
__INT8_TYPE__ n = 90;
std::cout<<typeid(n).name()<<" "<<n<<std::endl;
//and, &&
//and_eq, &=
//bitand, &
//bitor, |
//compl, ~
//not, !
//not_eq, !=
//or, ||
//or_eq, |=
//xor, ^
//xor_eq, ^=
if (1 < 2 and 4 > 1)
std::cout<<"And ..."<<std::endl;
return 0;
}
Reference