Sign in to follow this  

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by __fold
Can 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 this post


Link to post
Share on other sites
Quote:
Original post by Rattrap
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.


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 this post


Link to post
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.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this