小编5go*_*der的帖子

将函数指针转换为void(*)(),然后重铸为原始类型

这个问题仅用于测试目的,仅此而已.

我目前正在尝试使用不同数量的参数存储函数指针(这些参数可以有不同的类型).

基本上,我在C++ 11中编写了以下代码片段:

#include <functional>
#include <iostream>

void fct(int nb, char c, int nb2, int nb3) {
  std::cout << nb << c << nb2 << nb3 << std::endl;
}

template <typename... Args>
void call(void (*f)(), Args... args) {
  (reinterpret_cast<void(*)(Args...)>(f))(args...);
}

int main(void) {
  call(reinterpret_cast<void(*)()>(&fct), 42, 'c', 19, 94);
}
Run Code Online (Sandbox Code Playgroud)

我将void(*)(int, char, int, int)函数指针转换为通用void(*)()函数指针.然后,通过使用可变参数模板参数,我只需将函数指针重新设置为其原始类型,并使用一些参数调用该函数.

此代码编译并运行.大多数时候,它显示出良好的价值.但是,这段代码在Mac OS下给我一些Valgrind错误(关于未初始化的值),它有时会显示一些意外的垃圾.

==52187== Conditional jump or move depends on uninitialised value(s)
==52187==    at 0x1004E4C3F: _platform_memchr$VARIANT$Haswell (in /usr/lib/system/libsystem_platform.dylib)
==52187==    by 0x1002D8B96: __sfvwrite (in …
Run Code Online (Sandbox Code Playgroud)

c++ pointers function-pointers c++11

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

extern"C"内联函数

这段代码会导致未定义的行为吗?

header.h

#ifdef __cplusplus
extern "C"
{
#endif

inline int foo(int a)
{
    return a * 2;
}

#ifdef __cplusplus
}
#endif
Run Code Online (Sandbox Code Playgroud)

def.c

#include "header.h"

extern inline int foo(int a);
Run Code Online (Sandbox Code Playgroud)

use.c

#include "header.h"

int bar(int a)
{
    return foo(a + 3);
}
Run Code Online (Sandbox Code Playgroud)

main.cpp

#include <stdio.h>
#include "header.h"

extern "C"
{
    int bar(int a);
}

int main(int argc, char** argv)
{
    printf("%d\n", foo(argc));
    printf("%d\n", bar(argc));
}
Run Code Online (Sandbox Code Playgroud)

这是一个程序的例子,其中一个inline函数必须在C和C++中使用.如果def.c被删除并且foo没有在C中使用它会起作用吗?(这假设C编译器是C99.)

编译时,此代码有效:

gcc -std=c99 -pedantic -Wall -Wextra -c -o …
Run Code Online (Sandbox Code Playgroud)

c c++ inline-functions

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

“警告:...的操作可能未定义”用于三元运算——不是 if/else 块

这是我的代码:

int main() {
    static int test = 0;
    const int anotherInt = 1;
    test = anotherInt > test ? test++ : 0;
    if (anotherInt > test)
        test++;
    else
        test = 0;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这是我构建它时产生的警告:

int main() {
    static int test = 0;
    const int anotherInt = 1;
    test = anotherInt > test ? test++ : 0;
    if (anotherInt > test)
        test++;
    else
        test = 0;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

为什么 C++ 给我一个关于三元运算的警告,而不是正则if..else语句?

c++ ternary-operator

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

头文件中的内联与静态内联

要将inline函数定义放在C头文件中以便应该内联到多个其他单元的函数中,应该使用inline还是static inline应该使用?我一直在谷歌搜索一段时间,但到目前为止似乎没有简明的解释.

c static inline-functions

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

C 中的仅头文件和仅静态内联库

我在 C 中编写了仅包含头文件的小库和static-inline仅库。当应用于大型库时,这会是一个坏主意吗?或者是否有可能使用仅标头版本的运行时间会更快?好吧,不考虑明显的编译时间差异。

c static inline-functions header-only

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

不了解Java原子包中的CAS操作

我正在研究Java书籍中的并发包。我不太了解本书中有关CAS操作的内容。下面的代码示例是counter本书中的线程安全类。

public class Counter{
    private AtomicInteger count = new AtomicInteger();
    public void increment(){
        count.getAndIncrement();    //atomic operation
    }
    ....
}
Run Code Online (Sandbox Code Playgroud)

这是书中所说的。

实际上,即使诸如这样的方法也要getAndIncrement()花费几个步骤来执行。此实现现在是线程安全的,因此称为CAS。CAS代表“比较并交换”。大多数现代CPU都有一组CAS指令。现在正在发生的事情的基本概述如下:

  1. 存储在count中的值将被复制到一个临时变量中。
  2. 临时变量增加。
  3. 将当前计数的值与原始值进行比较。如果未更改,则将旧值替换为新值。

好吧,我明白它关于多个步骤的说法。我不太了解的是枚举步骤中正在发生的事情。

  1. 存储在count中的值将被复制到一个临时变量中。

该临时变量在哪里?它在主存储器中,注册吗?还是特定于CPU体系结构?

  1. 将当前计数的值与原始值进行比较。如果未更改,则将旧值替换为新值。

原始值存储在哪里?不能是临时变量。那个人正在被修改,对吧?我想念什么?

谢谢

java atomicity compare-and-swap

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

"显式"构造函数对重载决策的影响

为什么以下代码不能编译,当我在A类中的构造函数之前删除显式关键字时,它编译?

使用Visual Studio 2013:

enum E { e1_0, e1_1 };

template<typename T>
struct A
{
    A() {}
    explicit A(unsigned long) {}
    A(T) {}
};

struct B
{
    B() {}
    B(E) {}
};


void F(B) {};
void F(A<short>) {};

void test()
{
    F(e1_0);
}
Run Code Online (Sandbox Code Playgroud)

错误:

1>------ Build started: Project: exp_construct_test, Configuration: Debug Win32 ------
1>  exp_construct_test.cpp
1>e:\exp_construct_test\exp_construct_test.cpp(23): error C2668: 'F' : ambiguous call to overloaded function
1>          e:\exp_construct_test\exp_construct_test.cpp(19): could be 'void F(A<short>)'
1>          e:\exp_construct_test\exp_construct_test.cpp(18): or       'void F(B)'
1>          while trying to …
Run Code Online (Sandbox Code Playgroud)

c++ constructor visual-c++ overload-resolution

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

命名空间中的const全局变量

我知道static const class成员只能在标题中初始化.名称空间是否相同?例如,写入是否有效:

namehuman.hpp

namespace namehuman
{
   string const human("human");
}
Run Code Online (Sandbox Code Playgroud)

main.cpp

#include "namehuman.hpp"
cout << namehuman::human << endl;
Run Code Online (Sandbox Code Playgroud)

我想知道包括头文件在内的所有文件是否都有自己的string人工副本,或者人类是否是真正的全局变量(不会多次复制).为了避免每个包含文件的副本,我是否有义务使用extern

c++

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

做一些奇怪的事情的c ++匿名构造函数

此示例程序显示如何调用不同的构造函数,具体取决于是传入局部变量,全局变量还是匿名变量.这里发生了什么?

std::string globalStr;
class aClass{
public:
  aClass(std::string s){
    std::cout << "1-arg constructor" << std::endl;
  }
  aClass(){
    std::cout << "default constructor" << std::endl;
  }
  void puke(){
    std::cout << "puke" << std::endl;
  }
};

int main(int argc, char ** argv){
  std::string localStr;
  //aClass(localStr); //this line does not compile
  aClass(globalStr);  //prints "default constructor"
  aClass(""); //prints "1-arg constructor"
  aClass(std::string("")); //also prints "1-arg constructor"
  globalStr.puke(); //compiles, even though std::string cant puke.
}
Run Code Online (Sandbox Code Playgroud)

鉴于我可以调用globalStr.puke(),我猜测通过调用aClass(globalStr);,它正在创建一个名为globalStrtype 的局部变量aClass,而不是全局变量globalStr.调用aClass(localStr); …

c++ anonymous default-constructor

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

GNU make 自动依赖生成

最近在学习GNU make。以下是 GNU make 手册中自动生成依赖项的示例。我知道上面脚本的一般含义(它基本上是用命令用其他东西替换一些东西sed)。但我对命令中的细节有两个问题。\1.o首先,命令中的含义是什么?与位置参数有关吗?其次,为什么$*要放入一对,()因为$*它本身已经意味着目标文件名的主干?(我尝试不加括号,但没有成功。)

%.d: %.c
    $(CC) -M $(CPPFLAGS) $< > $@.$$$$;                  \
    sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
    rm -f $@.$$$$
Run Code Online (Sandbox Code Playgroud)

我尝试了以下方法,但没有成功。

%.d: %.c
    $(CC) -M $(CPPFLAGS) $< > $@.$$$$;                  \
    sed 's,$*\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@;     \
    rm -f $@.$$$$
Run Code Online (Sandbox Code Playgroud)

谁能解释一下背后的原因吗?谢谢。

linux sed gnu-make

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