我与Johannes Schaub就关键字进行了讨论.那里的代码是这样的:inline
namespace ... {
static void someFunction() {
MYCLASS::GetInstance()->someFunction();
}
};
Run Code Online (Sandbox Code Playgroud)
他说:
将其作为内联函数可以在可执行文件中保存代码大小
然而,约翰内斯表示明确指定它还有其他好处.不幸的是我不理解他们.例如,他说,"内联"允许您在程序中多次定义函数.,我很难理解(并找到参考).
所以
inline只是编译器的建议?inline?inline以减少可执行文件的大小,即使编译器(根据维基百科[我知道,错误的引用])应该自己找到这样的函数?还有什么我想念的吗?
在这个答案/sf/answers/293558891/中写道,"内联函数默认具有外部链接".但是,默认情况下不可能链接内联的内容.那么说内联函数有外部联系是什么意思呢?
我对从这段代码得到的结果感到困惑.在一个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)
在我的应用项目中 …
我正在尝试使用外部链接创建名称空间范围常量
// 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)
这甚至可能吗?
创建以下类后,编译失败,出现许多"重复符号"错误.实际错误不是很具描述性:
"复制符号__Zeq .... in:/Users/myusername/Library/Developer/Xcode/DerivedData/MyProject-asdfasfasdf..../Build/Intermediates/MyProject.build/Debug-iphonesimulator/MyTarget.build/Objects-normal /i386/MyClass.o"
上面相同的消息出现在许多不同的类中,并出现在编译结束时,所以我不知道问题是什么.
我检查了以下内容:
问题是什么?
#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) 请参阅以下代码.第一个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) #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拒绝了我的要求.