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

1

7

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

Quinze answered 23/9, 2015 at 10:18 Comment(0)
M
7

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.

Moberg answered 23/9, 2015 at 10:23 Comment(3)
Thanks. Edited post with what I think should be correct.Quinze
Thanks so much, spent ages searching for an answer! :)Quinze
@Rhys, glad I could helpMoberg

© 2022 - 2024 — McMap. All rights reserved.