Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ use templates to avoid compiler from checking a boolean

Tags:

Let's say I have a function:

template <bool stuff>
inline void doSomething() {
    if(stuff) {
        cout << "Hello" << endl;
    }
    else {
        cout << "Goodbye" << endl;
    }
}

And I call it like this:

doSomething<true>();
doSomething<false>();

It would pring out:

Hello
Goodbye

What I'm really wondering is does the compiler fully optimize this? When I call the templated function with true, will it create a function that just outputs "Hello" and avoids the if statement and the code for "Goodbye"?

This would be really useful for this one giant function I just wrote that's supposed to be very optimized and avoid as many unnecessary if statement checks as possible. I have a very good feeling it would, at least in a release build with optimizations if not in a debug build with no optimizations.

like image 327
iseletsky Avatar asked Aug 19 '12 22:08

iseletsky


People also ask

What are templates and how does the compiler interpret them?

A template is not a class or a function. A template is a “pattern” that the compiler uses to generate a family of classes or functions. In order for the compiler to generate the code, it must see both the template definition (not just declaration) and the specific types/whatever used to “fill in” the template.

Can we use template in C?

The main type of templates that can be implemented in C are static templates. Static templates are created at compile time and do not perform runtime checks on sizes, because they shift that responsibility to the compiler.

What is the role of compiler by templates?

Compiler creates a new instance of a template function for every data type. So compiler creates two functions in the above example, one for int and other for double. Every instance has its own copy of static variable. The int instance of function is called twice, so count is incremented for the second call.


1 Answers

Disclaimer: Noone can guarantee anything.

That said, this an obvious and easy optimization for any compiler. It's quite safe to say that it will be optimized away, unless the optimizer is, well, practically useless.

Since your "true" and "false" are constants, you are unambiguously creating an obvious dead branch in each class, and the compiler should optimize it away. Should is taken literally here - I would consider it a major, major problem if an "optimising" compiler did not do dead branch removal.

In other words, if your compiler cannot optimize this, it is the use of that compiler that should be evaluated, not the code.

So, I would say your gut feeling is correct: while yes, no "guarantees" as such can be made on each and every compiler, I would not use a compiler incapable of performing simplistic optimizations in any production environment, and of course not in any performance critical one. (In release builds of course).

So, use it. Any modern optimizing compiler will optimize it away because it is a trivial optimization. If in doubt, check disassembly, and if it is not optimized, change the compiler to something more modern.

In general, if you are writing any kind of performance-critical code, you must rely, at least to some extent, on compiler optimizations.

like image 140
Gerasimos R Avatar answered Oct 16 '22 11:10

Gerasimos R