内联调用不太可能失败,代码大小将增长[-Winline],但不使用内联

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

本文关键字:-Winline 代码 调用 失败 小将      更新时间:2023-10-16

c++新手。

下面是我的用户定义的fmiNode类:(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;
};

这是我的主要方法(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();
  }
}

如果我只实例化一个fmiNode对象,一切都很好。但是下面的3会引发警告:

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

我哪里做错了。

编辑:

所以我应该这样定义我的类:?
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;
}

欢呼,里斯

如果您在类定义中定义函数(在您的例子中是构造函数),则结果与使用inline关键字在类外部定义它相同,根据c++标准:

在类定义内定义的函数是内联函数

所以编译器得到了inline的提示,但由于警告消息中的原因,认为将构造函数内联到main是一个坏主意,所以它给了你警告。

Update:是的,您应该在EDIT中定义类以避免此警告。更好的是,将定义放入.cpp文件中,以避免多个定义错误。