小编Don*_*wik的帖子

使用const初始化constexpr, - int vs float

我想知道为什么整数ii在编译时初始化,但不是浮点数ff:

int main() {
  const int i = 1;
  constexpr int ii = i;

  const float f = 1.0;
  constexpr float ff = f;
 }
Run Code Online (Sandbox Code Playgroud)

这是我尝试编译时会发生的事情:

> g++ -std=c++11 test.cc
test.cc: In function ‘int main()’:
test.cc:6:24: error: the value of ‘f’ is not usable in a constant expression
   constexpr float ff = f;
                        ^
test.cc:5:15: note: ‘f’ was not declared ‘constexpr’
   const float f = 1.0;
Run Code Online (Sandbox Code Playgroud)

c++ constexpr

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

pdf设备无法使用unicode字体?

基本上,我在默认的R绘图设备上得到了我想要的方式,希腊字母等,然后我切换绘图设备创建一个pdf文件,我有"字体"问题.

例如:

> x<- 1:4
> y<-x^2
> plot(x,y)
> text(1,2, "\u03bc") #\u03bc == 'mu'
Run Code Online (Sandbox Code Playgroud)

一切正常,根据需要在默认的R绘图设备(图形窗口)上创建希腊字母'mu'.

但是,当我然后切换绘图设备以生成.pdf文件时:

> pdf()
> plot(x,y)
> text(1,2, "\u03bc")
Run Code Online (Sandbox Code Playgroud)

给出以下内容:

Warning messages:
1: In text.default(1, 2, "?") :
  conversion failure on '?' in 'mbcsToSbcs': dot substituted for <ce>
2: In text.default(1, 2, "?") :
  conversion failure on '?' in 'mbcsToSbcs': dot substituted for <bc>
3: In text.default(1, 2, "?") :
  font metrics unknown for Unicode character U+03bc
4: In text.default(1, 2, "?") :
  conversion failure …
Run Code Online (Sandbox Code Playgroud)

pdf fonts r

5
推荐指数
0
解决办法
1082
查看次数

POSIX 消息队列位于哪里(Linux)?

man 7 mq_overviewPOSIX 表示“...系统上的消息队列可以使用通常用于文件的命令(例如 ls(1) 和 rm(1))来查看和操作。” 例如,我能够使用 mqd_t 作为文件描述符进行读取,如下所示:

#include <iostream>
#include <fcntl.h>
#include <mqueue.h>
#include <unistd.h>
#include <stdlib.h>

int main(int argc, char **argv) {
  if (argc != 2) {
    std::cout << "Usage: mqgetinfo </mq_name>\n";
    exit(1);
  }
  mqd_t mqd = mq_open(argv[1], O_RDONLY);

  struct mq_attr attr;
  mq_getattr(mqd, &attr);
  std::cout << argv[1] << " attributes:"
        << "\nflag: " << attr.mq_flags
        << "\nMax # of msgs: " << attr.mq_maxmsg
        << "\nMax msg size: " << attr.mq_msgsize
        << "\nmsgs now in queue: …
Run Code Online (Sandbox Code Playgroud)

linux posix message-queue

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

Python C++浮点数学表示错误

在C++中,

double x = 1.0; 
double y = x / 3.0; 
if (x == y * 3.0)
  cout << " They are equal!" ; 
else
  cout << " They are NOT equal." ;
Run Code Online (Sandbox Code Playgroud)

将打印

‘They are NOT equal.’ 
Run Code Online (Sandbox Code Playgroud)

正如预期的那样,由于1/3的非精确表示为(二进制)数量的有限大小的尾数.但是在Python中(在https://repl.it/repls/MessyJaggedMouse上),

x = 1.0
y = x / 3.0
x == y * 3.0
Run Code Online (Sandbox Code Playgroud)

版画

True
Run Code Online (Sandbox Code Playgroud)

何时以及为什么Python会偏离上述预期的行为?编辑:Python不会离开 - 请参阅下面的答案.

c++ python floating-point

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

局部(块)函数decln与defn返回类型不匹配; 在C++中进行诊断?

该程序位于名为localFunc.c的文件中:

#include <stdio.h>

// int f(int);  // Global forward declaration.

int main() {
   int f(int);  // Local forward declaration.
   printf("%d\n", f(1));
}

double f(int i) {
   return 1.0;
}
Run Code Online (Sandbox Code Playgroud)

编译via gcc localFunc.c给出:

localFunc.c:10:8: error: conflicting types for ‘f’
 double f(int i) {
        ^
localFunc.c:6:7: note: previous declaration of ‘f’ was here
    int f(int);  // Local forward declaration.
Run Code Online (Sandbox Code Playgroud)

但编译通过g++ localFunc.c,没有错误,运行可执行文件的结果是:4195638.

评论本地前向声明,并启用全局前向声明:int f(int);,gcc和g ++都会出现类似于上面的错误,正如预期的那样(由我).

所以我的问题是,为什么看起来g ++没有在本地声明的函数上看到冲突类型(模糊声明)?

don@HAL:~/UNIX/CS213$ gcc --version
gcc (Ubuntu 5.2.1-22ubuntu2) 5.2.1 20151010
Run Code Online (Sandbox Code Playgroud)

c c++ gcc

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

标签 统计

c++ ×3

c ×1

constexpr ×1

floating-point ×1

fonts ×1

gcc ×1

linux ×1

message-queue ×1

pdf ×1

posix ×1

python ×1

r ×1