GuLearn
GuLearn

Reputation: 1864

Will long, complex template member method affect the performance?

I know that a long complicated inline method may affect the performance (although a short simple one may improve the performance). However, the methods of a template class have to be defined in the header file. Are they inlined? If so, will there be any affection on the performance? Should I have long, complicated method in a template class?

Upvotes: 1

Views: 76

Answers (2)

Pete Becker
Pete Becker

Reputation: 76245

You should write the code you need and leave it to the compiler to figure out how best to use it. Defining template functions in a header file does not make them inline; the compiler might inline them or it might not.

Upvotes: 1

sth
sth

Reputation: 229573

The compiler will inline the function if it thinks it will improve performance. If it doesn't think it will improve performance, it won't inline it, even if it's defined in the header. If it doesn't actually inline the function it will also take care that the linker doesn't get confused when this function shows up in different compilation units.

The same applies even when you declare a function inline.

So the compiler does what is best, you don't have to worry about it.

Upvotes: 5

Related Questions