# Yet another inline question

This topic is 4561 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Ok, I thought I understood most things about inline but then I checked the asm by accident. Consider this code in a Vector2 class
template <typename T>
Vector2<T> Vector2<T>::operator-(const Vector2<T>& v) const
{
return Vector2<T>(data_[0] - v.data_[0],
data_[1] - v.data_[1]);
}


Is it possible to inline this function? I thought so. I believe that my compiler settings are correct. (I use VC.NET 2003)

##### Share on other sites
Did try putting the inline keyword on the function definition and not its declaration, also if you define any member function inside a class/struct definition its implicitly inline meaning you don't need to add the keyword inline to give hints to the compiler.

Lastly a tip, build the general arithmetic operators in terms of arithmetic assignment operators e.g.:

struct foo {   foo& operator+=(const foo& f) { /* ... */ return *this; }     foo operator+(const foo& f) const { return foo(*this) += f; }};

##### Share on other sites
Inline is just a suggestion to the compiler. It may ignore it at will. It may ignore it in some places and not in others. It will always ignore inline in Debug builds.

##### Share on other sites
VC++ don't care at all about the inline keyword. There is a keyword called __forceinline though but that is also only a suggestion. If I compile it with __forceinline I get a message that the function can't be inlined. I'm pretty sure my compiler settings are right. I'm a bit suspicious about the /EHsc-flag. It is suppose to be the same as another set of flags that are required for inlining but it lacks a bit of documentation. Can it have something to do with the template syntax?

##### Share on other sites
Nope, __forceinline isn't a "hint" - it really does try to force the function inline, however it won't make your compiler do the impossible. There are just some situations that simply cannot be inlined. These include recursion and some loops.

It dosn't have anything to do with /EHsc.

Anyway - I wouldn't worry about inlining it. Just drop your function "inline" in the class definition, and all will be well.

##### Share on other sites
Quote:
 Original post by __foldCan it have something to do with the template syntax?

No, considering that template definitions need to be in the same translation unit they where declared in (unless a compiler supports the standard export keyword) meaning it needs to be in the header as do inlineable functions (be templated or not).

##### Share on other sites
Also, an optimizing compiler like Visual Studio might choose to inline a function that isn't marked inline if it "feels" that it should be.

##### Share on other sites
Quote:
 Original post by RattrapAlso, an optimizing compiler like Visual Studio might choose to inline a function that isn't marked inline if it "feels" that it should be.

Yes, almost all small functions (like gets and sets) become inlined. It can also choose to inline a function at one place in the code and then choose not to inline the same function in another place in the code. In my case, I just can't see why it can't be inlined at all.

##### Share on other sites

This topic is 4561 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.