Post

C++ Tips - Part 02

unevaluated context, noexcept, zero cost abstraction

C++ Tips - Part 02

My C++ Tips

Part 01


Unevaluated context

  • The expression is parsed and analyzed, but NOT executed.
  • No opcode generated
1
2
3
int x = 10;
size_t s = sizeof(x++);
// x is still 10
  • sizeof
  • decltype
  • typeid
  • noexcept

What is noexcept?

1
2
3
void foo() noexcept; // guarantees no throw

bool b = noexcept(bar()); // // true if bar() guaranteed not to throw

Zero cost abstraction

  • What you dont use, you dont pay for
  • What you do use, you couldn’t hand code better
  • Code will be slower to compile

Adding high level programming concept, do not come with run-time cost, only compile time cost.

Example:

1
2
3
4
5
6
7
8
9
template<typename T>
T add(T a, T b) 
{
    return a + b;
}

// with zero-cost abstractions, 
// No function call — it’s inlined.
int result = a + b;
This post is licensed under CC BY 4.0 by the author.