Post

C++ Tips - Part 01

std::vector , func pointer

C++ Tips - Part 01

My C++ Tips

Part 01


std::vector< bool >

  • special vector
  • space-efficient vector
  • each element occupies a SINGLE BIT instead of sizeof(bool) bytes.
  • std::vector<bool> is not contiguous

Narrowing conversion (with std::initializer_list)

1
2
3
4
5
6
7
8
9
float f = 3.14f;
int i = f;  // narrowing: decimal part is lost

double d = 3.45678314;
float fx = d; // narrowing: precision (might) loss

//Narrowing and std::initializer_list
int x1 = 3.14; // OK
int x2{3.14}; // ERROR

Example of func pointer

1
2
3
4
5
6
7
8
using func = int(int);
int foo(int) { return 0; }

int main() 
{
	func* fp = &foo;
	int (*fpp)(int) = &foo;
}
This post is licensed under CC BY 4.0 by the author.