当调用一个非inline与从C程序2个的整数或指针参数的功能,它们是在寄存器中传递rdi和rsi.编译是否可以以这样的方式完成,即inline函数的整数/指针参数也在寄存器中传递(而不是在堆栈中)?
假设我们有以下代码:
inline void DoSome()
{
cout << "do some" << endl;
}
int main()
{
void (*pDoSome)() = DoSome;
DoSome(); // one
pDoSome(); // two
}
Run Code Online (Sandbox Code Playgroud)
对于上述代码,我们有三种可能的情况:
现在我想知道上述哪种情况属实?
我想用lambda函数调用一个方法(对于这个例子std :: thread构造函数),传递int值:
int a=10;
std::thread _testThread = thread([a](int _a){
//do stuff using a or _a ?
});
_testThread.detach();
Run Code Online (Sandbox Code Playgroud)
我不知道如何正确编写这样的函数,我得到这个错误:C2064:term不计算为0参数的函数
从理论上讲,inline函数在 C 中具有内部/静态链接,也就是说,它们仅在单个翻译单元中可见。因此,在两个单独的文件中定义的内联函数不应该能够看到彼此,并且两者都有自己的地址空间。
我正在尝试使用以下文件
***cat a.c***
#include <stdio.h>
inline int foo()
{
return 3;
}
void g()
{
printf("foo called from g: return value = %d, address = %#p\n", foo(), &foo);
}
***cat b.c***
#include <stdio.h>
inline int foo()
{
return 4;
}
void g();
int main()
{
printf("foo called from main: return value = %d, address = %#p\n", foo(), &foo);
g();
return 0;
}
gcc -c a.c
gcc -c b.c
gcc -o a.out a.o b.o
b.o: In …Run Code Online (Sandbox Code Playgroud) 我使用VB6作为应用程序.
是否可以强制编译器内联函数?
或者是否有一个加载项实现相同的功能?
我希望通过在使用它的每个点重复代码而不是作为函数列出一次代码来使我的代码变得难以入侵.
希望有人能回答我的问题!
为什么不能在 .cpp 文件中内联函数或类成员函数定义?例如,在我下面的测试示例中,如果我尝试在 cpp 文件中内联函数或类成员定义,链接器会给我一个未定义的引用错误:
测试.h
void print();
class A
{
public:
void print() const;
};
Run Code Online (Sandbox Code Playgroud)
测试.cpp
#include <test.h>
inline void print()
{
// implementation
}
inline void A::print() const
{
// implementation
}
Run Code Online (Sandbox Code Playgroud)
主程序
#include <test.h>
int main(int argc, char** argv)
{
print(); // undefined reference to `print()'
A a;
a.print(); // undefined reference to `A::print() const'
return 0;
}
Run Code Online (Sandbox Code Playgroud)
#include <iostream>
#include <functional>
int main(){
int a = 10;
std::function<int(int)> functionPointer = [a](int a)-> int{ return a + a + 100 ; };
int returnValue = functionPointer(50);
std::cout<<returnValue<<endl;
}
Run Code Online (Sandbox Code Playgroud)
我原本期望10 + 50 + 100 = 160,但是输出是10 + 10 + 100 =120。我可以做些改变以获得160,同时保持变量名称不变吗?
我正在尝试访问多个 C++ 文件中的一个头文件。头文件定义如下:
#ifndef UTILS
#define UTILS
void toUpper(string &str) {
for(unsigned int i=0; i<str.length(); i++) {
str.at(i) = toupper(str.at(i));
}
}
void toLower(string &str) {
for(unsigned int i=0; i<str.length(); i++) {
str.at(i) = tolower(str.at(i));
}
}
#endif // UTILS
Run Code Online (Sandbox Code Playgroud)
据我所知,这不应该返回多个定义,因为我限制代码被定义多次。我还尝试使用预编译指令“#pragma Once”
我有一个想法,如果代码片段很小,使用“inline 关键字”全局定义内联函数(普通函数)可以提高性能。我有一个疑问:“类内部定义的成员函数如何也提供相同的性能并被视为内联? ”
要使函数内联,应该在函数原型或函数定义中添加关键字inline,还是在函数声明和函数定义中均添加inline关键字。