Overhead of `using`

  • A+
Category:Languages

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.


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

No there is not.

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

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: