标签: inline-functions

参数传递给Linux C内联函数

当调用一个非inline与从C程序2个的整数或指针参数的功能,它们是在寄存器中传递rdi和rsi.编译是否可以以这样的方式完成,即inline函数的整数/指针参数也在寄存器中传递(而不是在堆栈中)?

c inline-functions

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

这段代码将如何编译

假设我们有以下代码:

inline void DoSome()
{
    cout << "do some" << endl;
}

int main()
{
    void (*pDoSome)() = DoSome;

    DoSome(); // one
    pDoSome(); // two
}
Run Code Online (Sandbox Code Playgroud)

对于上述代码,我们有三种可能的情况:

  1. 一个将内联,两个不会
  2. 将内联一个和两个
  3. 一个和两个不会内联(因为我们采用了函数的地址)

现在我想知道上述哪种情况属实?

c++ function-pointers inline-functions

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

如何用参数写lambda函数?C++

我想用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参数的函数

c++ argument-passing inline-functions stdthread

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

C 中内联函数内部链接的澄清

从理论上讲,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)

c inline-functions linkage

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

VB6(erk) - 内联函数?

我使用VB6作为应用程序.

是否可以强制编译器内联函数?

或者是否有一个加载项实现相同的功能?

我希望通过在使用它的每个点重复代码而不是作为函数列出一次代码来使我的代码变得难以入侵.

希望有人能回答我的问题!

vb6 inline-functions

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

为什么不能在 .cpp 文件中内联函数定义?

为什么不能在 .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)

我在这里阅读了一些答案,但我仍然不确定它是如何工作的。

c++ linker inline inline-functions

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

如何在C ++中的lambda函数中传递相同名称的局部变量和参数(使用此关键字)?

#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++ lambda functional-programming inline-functions c++11

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

多个cpp文件中的一个头文件:多个定义

我正在尝试访问多个 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”

c++ one-definition-rule inline-functions

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

如何将多行代码的内联函数视为单行?

我有一个想法,如果代码片段很小,使用“inline 关键字”全局定义内联函数(普通函数)可以提高性能。我有一个疑问:“类内部定义的成员函数如何也提供相同的性能并被视为内联? ”

c++ inline-functions

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

如何在C ++中内联函数

要使函数内联,应该在函数原型或函数定义中添加关键字inline,还是在函数声明和函数定义中均添加inline关键字。

c++ inline-functions

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