相关疑难解决方法(0)

内联的目的是什么?

我与Johannes Schaub就关键字进行了讨论.那里的代码是这样的:inline

namespace ... {
    static void someFunction() {
        MYCLASS::GetInstance()->someFunction();
    }
};
Run Code Online (Sandbox Code Playgroud)

他说:

将其作为内联函数可以在可执行文件中保存代码大小

但根据我在这里这里的发现,不需要,因为:

  • [内联]仅在编译器的成本/收益分析显示为有利可图时才会出现
  • 主流C++编译器(如Microsoft Visual C++和GCC)支持一个选项,允许编译器自动内联任何合适的函数,甚至那些未标记为内联函数的函数.

然而,约翰内斯表示明确指定它还有其他好处.不幸的是我不理解他们.例如,他说,"内联"允许您在程序中多次定义函数.,我很难理解(并找到参考).

所以

  1. 是否inline只是编译器的建议?
  2. 当你有一个小功能时我是否应该明确说明(我猜1-4指令?)
  3. 写作有什么其他好处inline
  4. 是否需要声明inline以减少可执行文件的大小,即使编译器(根据维基百科[我知道,错误的引用])应该自己找到这样的函数?

还有什么我想念的吗?

c++ compiler-construction inline

15
推荐指数
2
解决办法
1663
查看次数

内联函数和外部链接

在这个答案/sf/answers/293558891/中写道,"内联函数默认具有外部链接".但是,默认情况下不可能链接内联的内容.那么说内联函数有外部联系是什么意思呢?

c++

7
推荐指数
1
解决办法
2110
查看次数

本地静态变量被多次实例化,为什么?

我对从这段代码得到的结果感到困惑.在一个dll中,当静态变量初始化时,计数器递增.然后当执行main时我读了这个计数器,但我得到的是0而不是1.有人可以向我解释一下吗?

在我的动态库项目中:

// Header file
class Foo {
   int i_ = 0;

   Foo(const Foo&) = delete;
   Foo& operator= (Foo) = delete;

   Foo()
   {
   }

public:
   void inc()
   {
      ++i_;
   }

   int geti()
   {
      return i_;
   }

   static Foo& get()
   {
      static Foo instance_;
      return instance_;
   }

   Foo( Foo&&) = default;
   Foo& operator= (Foo&&) = default;
};

int initialize()
{
   Foo::get().inc();
   return 10;
}

class Bar
{
   static int b_;

};

// cpp file
#include "ClassLocalStatic.h"


int Bar::b_ = initialize();
Run Code Online (Sandbox Code Playgroud)

在我的应用项目中 …

c++ c++11

5
推荐指数
2
解决办法
3650
查看次数

如何使用外部链接在命名空间范围内定义常量double?

我正在尝试使用外部链接创建名称空间范围常量

// in some include file:

namespace foo 
{
    constexpr double bar() { return 1.23456; } // internal linkage
    constexpr double baz = 1.23456;            // internal linkage
    const double bing = 1.23456;               // internal linkage
}
Run Code Online (Sandbox Code Playgroud)

这甚至可能吗?

c++ constexpr c++11

5
推荐指数
1
解决办法
955
查看次数

为什么这不会标记操作符重载,因为内联会导致重复的定义错误?

创建以下类后,编译失败,出现许多"重复符号"错误.实际错误不是很具描述性:

"复制符号__Zeq .... in:/Users/myusername/Library/Developer/Xcode/DerivedData/MyProject-asdfasfasdf..../Build/Intermediates/MyProject.build/Debug-iphonesimulator/MyTarget.build/Objects-normal /i386/MyClass.o"

上面相同的消息出现在许多不同的类中,并出现在编译结束时,所以我不知道问题是什么.

我检查了以下内容:

  1. 使用它的类包括Listening.hpp文件.
  2. 此类的唯一定义是在此文件中.

问题是什么?

#ifndef Listening_hpp
#define Listening_hpp
#include <stdio.h>
#include "EAction.hpp"

class Listening{
private:
    int _emitterId;
    int _listenerId;
    std::string _eventName;
    EAction* _actionToTake; //not owned.

protected:

public:

    Listening(int emitterId,int listenerId,std::string eventName,EAction* actionToTake) : _emitterId(emitterId),_listenerId(listenerId),_eventName(eventName){
        _actionToTake = actionToTake;
    }

    int getEmitterId()const{
        return _emitterId;
    }

    int getListenerId()const{
        return _listenerId;
    }

    std::string getEventName()const{
        return _eventName;
    }

    EAction* getAction()const{
        return _actionToTake;
    }
};

bool operator==(const Listening &first,const Listening &other)
{
    bool result = false;

    if(first.getEmitterId() == other.getEmitterId()){
        if(first.getListenerId() …
Run Code Online (Sandbox Code Playgroud)

c++ inline multiple-definition-error c++11

5
推荐指数
1
解决办法
212
查看次数

模板专业化:我们可以部分实现特殊情况

请参阅以下代码.第一个MyClass <>有两个函数(func1和func2).然后我想在func1中为MyClass做一些特殊的事情,但不是func2.看起来我必须再次为func2键入代码.我想知道是否有办法解决这个问题?谢谢

#include <iostream>

using namespace std;

template <class T>
class MyClass {
public:
    void func1(){
    cout<<"default: func1"<<endl;
     }
    void func2(){
    cout<<"default: func2"<<endl;
     }
private:
    T haha;
};

template <>
class MyClass<double> {
public:
    void func1(){
    cout<<"special: func1"<<endl;
    }
};

int main()
{
    MyClass<int> intclass;
    intclass.func1();
    intclass.func2();

    MyClass<double> doubleclass;
    doubleclass.func1();
    doubleclass.func2();  // error 'class MyClass<double>' has no member named 'func2'
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ templates

3
推荐指数
1
解决办法
528
查看次数

gcc拒绝内联请求

#include <iostream>

using namespace std;

inline int square(int n)
{
    return n * n;
}

int main(void)
{
    cout << square(2) << endl;
}
Run Code Online (Sandbox Code Playgroud)

通过使用gcc编译代码,我发现编译器仍然将square函数视为简单函数而不是inline.

(我查看了汇编代码,在那里我看到了对square函数的调用.这就是我所知道的并不是将它视为内联函数.)

为什么即使它是一个简单的函数,否认我的内联函数请求?如果gcc拒绝内联这样一个简单的函数那么内联关键字的用途是什么?

我可以强制内联函数; 那不是问题.我只是想知道为什么gcc拒绝了我的要求.

c++ gcc inline

1
推荐指数
1
解决办法
140
查看次数