Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overhead of `using`

For my problem, I can use the using directive in two ways. They basically boil down to these options:

template<typename U>
struct A {
private:

    // Define our types
    using WrapperType = Wrapper<U>;

public:

    U *operator()(U *g) const {
       // TODO: use WrapperType 
    }
};

OR:

struct B {

    template <typename U>   
    U *operator()(U *g) const {

       // Define the types here instead.
       using WrapperType = Wrapper<U>;

       // TODO: use WrapperType 
    }
};

In both cases, there will be other class template parameters. So B will still have template parameters, even though it doesn't look like it in this simplified example.

My question is:

Is there any overhead of defining a type locally like in B? (when compared to A)?

It isn't clear to me how the type declaration affects the generated code. The code must run in real time, and this will be the core of the codebase. So if there is any overhead whatsoever, I cannot use B.

That being said, B IS preferable in our case, because I would ideally like to call this code with a variety of types. And yes, this really does need to be in a class. I have just simplified the example extremely.

like image 993
bremen_matt Avatar asked Apr 23 '18 08:04

bremen_matt


People also ask

What is meaning of the overhead?

What Is Overhead? Overhead refers to the ongoing business expenses not directly attributed to creating a product or service. It is important for budgeting purposes but also for determining how much a company must charge for its products or services to make a profit.


1 Answers

Is there any overhead [on the generated code] of defining a type locally like in B?

No there isn't any.

Defining a type alias (what you do with using WrapperType = Wrapper<U>;) only affects compilation and is completely removed once run-time begins.

like image 139
YSC Avatar answered Oct 13 '22 18:10

YSC