小编Tom*_*Tom的帖子

布尔值的排序

在C++或<stdbool.h>C99下,如何<为布尔值定义小于运算符?

或者,解释此代码的行为:

#ifndef __cplusplus
#include <stdbool.h>
#endif
#include <stdio.h>

int main() {
    bool b = -1;
    if(b < true) {
        printf("b < true\n");
    }
    if(b < false) {
        printf("b < false\n");
    }
    if(true < false) {
        printf("true < false\n");
    }
    if(false < true) {
        printf("false < true\n");
    }
}
Run Code Online (Sandbox Code Playgroud)

在MSVC版本10下,编译为C++代码,编译为C代码的GCC 4.6.3-ubuntu5和编译为C++代码的G ++ 4.6.3-1ubuntu5,所有你得到的是

false < true
Run Code Online (Sandbox Code Playgroud)

也就是说,以下不等式都是false:

(bool)-1 < true
(bool)-1 < false
true < false
Run Code Online (Sandbox Code Playgroud)

以下是true:

false < true
Run Code Online (Sandbox Code Playgroud)

c c++ boolean comparison-operators

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

Spyder默认模块导入列表

我正在尝试设置一个稍微定制的Spyder版本.当Spyder启动时,它会自动导入一长串模块,包括来自matplotlib,numpy,scipy等的东西.有没有办法将我自己的模块添加到该列表中?

如果它有所作为,我正在使用Python(X,Y)Windows安装程序提供的Spyder配置.

python import module spyder

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

GNU Fortran - 控制符号案例

有没有办法控制GNU Fortran 4.8发出的符号?

较旧的版本(例如3.4)分别使用了源和大写的情况-fcase-lower,-fcase-preserve并且-fcase-upper强制小写,但这些似乎已被丢弃.是否有一些新的控制方式?

编辑

我正在尝试将一个大型的,混合的C/Fortran代码库从英特尔编译器移植到GNU编译器.

我知道我们可以BIND(C, name='...')用来给出一个特定于案例的符号名称.但是,这还有其他影响.考虑这个C函数:

void print(char *str, size_t len) {
    for(int ii = 0; ii < len; ii++) {
        putchar(str[ii]);
    }
    putchar('\n');
}
Run Code Online (Sandbox Code Playgroud)

我们可以从这样的Fortran程序中调用它:

program test
    implicit none
    interface
        subroutine printstr(str)
            character :: str(*)
        end subroutine
    end interface

    call printstr("Hello, world.");
end
Run Code Online (Sandbox Code Playgroud)

如果C函数名称不是全部小写(PrintStr比如说),那么我们可能会尝试修复Fortran程序,如下所示:

program test
    implicit none
    interface
        subroutine printstr(str) bind(C, name='PrintStr')
            use iso_c_binding
            character :: str(*)
        end subroutine
    end interface

    call printstr("Hello, world.");
end
Run Code Online (Sandbox Code Playgroud)

但这不起作用,因为 …

fortran gfortran

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

decltype比较

有没有办法比较decltypeC++ 11 的结果?

换句话说,为什么这段代码无效:

template<typename T, typename U>
void func(T& t, U& u) {
    if(decltype(t) == decltype(u)) {
        // Some optimised version for this case
    } else {
        // A more general case for differing types
    }
}
Run Code Online (Sandbox Code Playgroud)

我知道在某些情况下,这个特殊问题可以通过部分模板专业化来解决; 我的问题是关于decltypes的比较.

编辑:在试图通过SFINAE提供免费功能默认值的过程中出现了问题.或许更好的问题是为什么这是无效的:

template<bool B>
bool SomeFunction() { ... }

template<typename T, typename U>
bool SomeFunctionWrapper(T& t, U& u) {
    SomeFunction<decltype(t) == decltype(u)>();
}
Run Code Online (Sandbox Code Playgroud)

我已经找到了另一个解决方案(根本不涉及模板),但在一个阶段我尝试了这个:

// If it exists, the free function is defined as
// bool …
Run Code Online (Sandbox Code Playgroud)

c++ templates decltype c++11

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

处理未实例化的模板函数

以下代码在Visual C++ 2013中编译,但不在G ++ 4.8.2下编译:

template<class T>
int MyFunc(T& t)
{
    return static_cast<int>(CCodes::blah);
}

template<>
int MyFunc(float& t)
{
    return 0;
}

int main() {
    float f = 10.f;
    return MyFunc(f);
}
Run Code Online (Sandbox Code Playgroud)

Visual C++似乎忽略了通用模板函数,因为只MyFunc<float>使用了特化.无论如何,G ++解析了一般函数,并发现尚未定义CCodes枚举.

哪个是对的?或者是这个实现定义的?

c++ compiler-bug template-function

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

GNU Fortran和C互操作性

我有一个大的,混合的C/Fortran代码库,目前使用Windows上的英特尔工具编译.我被要求将它移植到Linux上的GNU工具.或多或少随机,我选择了4.8版本.

从Fortran调用C函数时,互操作性通常如下所示:

// C code:
void PRINTSTR(char *str, size_t len) {
    for(int ii = 0; ii < len; ii++) {
        putchar(str[ii]);
    }
    putchar('\n');
}

!Fortran code:
program test
implicit none
call printstr("Hello, world.")
end
Run Code Online (Sandbox Code Playgroud)

英特尔Fortran编译器始终生成大写符号,因此可以正常工作.但GNU Fortran编译器总是生成小写符号,因此存在链接器错误.

GNU Fortran编译器曾经有一个叫做-fcase-upper生成大写符号的选项,但是看起来它太可配置了,每个人的好处都被删除了(我不确定什么时候).

可以使用该ISO_C_BINDING工具强制编译器生成区分大小写的名称:

program test
interface
subroutine printstr(str) bind(C, name='PRINTSTR')
character :: str(*)
end subroutine
end interface

call printstr("Hello, world.")
end
Run Code Online (Sandbox Code Playgroud)

这解决了链接器错误,但它改变了字符串参数的处理方式; 不再提供length参数.因此,要使用此方法,我不仅必须为当前以这种方式工作的每个函数添加接口定义,而且还必须在每次调用此类函数时更改字符串的处理方式,确保所有字符串是以null结尾的.

我可以通过小写来完成所有这些函数,但当然英特尔编译器仍会生成大写符号,这样就会破坏现有的构建.

由于有大约2,000个这样的功能,这似乎是不可行的工作量.所以,我的问题是:如何在不改变函数调用语义的情况下解决链接错误,并且不使用英特尔编译器破坏现有构建?

c c++ fortran language-interoperability gfortran

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

main 之外的分段错误

我正在处理一个大型的混合 C++/Fortran 项目。目前,可执行文件在启动时立即出现段错误,然后到达mainAFAICT。实际上在加载共享库之前。

一些输出:

$ ./myprog
Segmentation fault (core dumped)
$ gdb ./myprog core
GNU gdb (Ubuntu 7.7-0ubuntu3) 7.7
Copyright (C) 2014 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For …
Run Code Online (Sandbox Code Playgroud)

c++ linker fortran segmentation-fault

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

如何使用 swagger-ui 在 swagger 文档中嵌入图像?

我试图在我的 swagger 部分description的字段中包含一个图像info,该图像通过 Swagger-UI 显示。到目前为止我已经尝试过 GFM:

...
  "info": {
    "description": "![alt text][/static/img/image.png]"
  }
...
Run Code Online (Sandbox Code Playgroud)

和普通的旧 HTML:

...
  "info": {
    "description": "<img alt=\"alt text\" src=\"/static/img/image.png\">"
  }
...
Run Code Online (Sandbox Code Playgroud)

但这两者都只是按照给定的方式呈现字符串,而不显示图像。

有没有办法做到这一点?

markdown swagger-ui openapi

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

在C中重新解释内存的正确方法是什么?

很久以前,我在C中做过类似事情的次数已经丢失了:

struct foo f;
struct foo* pf = &f;
char* pc = (char*) pf;
transmit(pc, sizeof(f));
Run Code Online (Sandbox Code Playgroud)

也许:

char* buffer[1024];
receive(buffer, 1024);
float values[256];
for(int ii = 0; ii < 256; ii++) {
    float* pf = (float*)(buffer + ii*4);
    values[ii] = *pf;
}
Run Code Online (Sandbox Code Playgroud)

或者可能:

uint32_t ipAddress = ...;
uint8_t* p = (uint8_t*)&ipAddress;
uint8_t octets[4] = {p[0], p[1], p[2], p[3]};
printf("%d.%d.%d.%d\n", octets[0], octets[1], octets[2], octets[3]);
Run Code Online (Sandbox Code Playgroud)

我刚刚发现通过转换为另一种指针类型来重新解释这样的内存会调用未定义的行为.然而,所有上述例子都是绝对必要的.这样做的正确方法是什么?

c memory pointers undefined-behavior

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

git 裸仓库、工作树和跟踪分支

我正在使用一个代码库,我需要为了不同的目的同时在多个分支上工作。所以我克隆到一个裸存储库,然后设置一些工作树:

git clone --bare ssh://git@git.example.com/project/repo repo.git
cd repo.git
git worktree add ../branch-1 branch-1
git worktree add ../branch-2 branch-2
... someone else creates branch-3 and pushes is ...
git fetch origin +refs/heads/*:refs/heads/* --prune
git worktree add ../branch-3 branch-3
Run Code Online (Sandbox Code Playgroud)

现在工作branch-3树没有设置为跟踪远程树并试图让它这样做,我陷入了可怕的混乱。

$ cd ../branch-3
$ git branch -u origin/branch-3
error: the requested upstream branch 'origin/refs/heads/feature/SW-5884-move-database-container-to-alpine-base-2' does not exist
hint: ...<snip>
$ git fetch +refs/heads/*:refs/remotes/origin/* --prune
$ git branch -u origin/branch-3
fatal: Cannot setup tracking information; starting point 'origin/feature/SW-5884-move-database-container-to-alpine-base-2' is not a …
Run Code Online (Sandbox Code Playgroud)

git git-bare git-branch git-worktree

4
推荐指数
3
解决办法
3090
查看次数