Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

inlining failed in call to unlikely and code size would grow [-Winline] but not using inline

Tags:

c++

inlining

very new to C++.

Here is my user defined fmiNode class: (fmi.h)

class fmiNode
{
public:
    fmiNode(std::string NodeName,int Address)
    {
        this->name = NodeName;
        this->address = Address;
    }

    std::string GetName()
    {
    return this->name;
    }

    int GetAddress()
    {
    return this->address;
    }

private:
    std::string name;
    int address;
};

Here is my main method (fmi.c)

int main (int argc, char *argv[])
{
  fmiNode node1("NodeA",4);
  fmiNode node2("NodeB",6);
  fmiNode node3("NodeC",8);
  fmiNode node4("NodeD",10);

  while(1)
  {
      MainLoop();
  }
}

If I only instantiate one fmiNode object everything is fine. but the following 3 causes a warning to be thrown:

 warning: inlining failed in call to ‘fmiNode::fmiNode(std::string, int)’: call is unlikely and code size would grow [-Winline]

What am I doing wrong here.

EDIT:

So I should define my class like this:?

class fmiNode
{
public:
    fmiNode(std::string NodeName,int Address);

    std::string GetName()
    {
    return this->name;
    }

    int GetAddress()
    {
    return this->address;
    }

private:
    std::string name;
    int address;
};

fmiNode::fmiNode(std::string NodeName,int Address)
{
    this->name = NodeName;
    this->address = Address;
}

Cheers, Rhys

like image 408
Rhys Goodwin Avatar asked Mar 15 '23 16:03

Rhys Goodwin


1 Answers

If you define a function (constructor, in your case) inside the class definition, the the result is the same as defining it outside of the class with inline keyword, as per the c++ standard:

7.1.2.3 A function defined within a class definition is an inline function

So the compiler gets the inline hint, but thinks that inlining the constructor into main is a bad idea because of the reasons in the warning message, so it gives you the warning.

Update: yes, you should define the class as in your EDIT to avoid this warning. Better yet, put the definition into the .cpp file to avoid multiple definition errors.

like image 148
SingerOfTheFall Avatar answered May 10 '23 05:05

SingerOfTheFall