小编Cás*_*nan的帖子

编译器警告 - 建议用作真值的赋值括号

当我尝试编译下面的代码时,我收到此警告:

warning: suggest parentheses around assignment used as truth value

为什么会这样?我相信这是一个相当普遍的习语.我甚至在我的代码中使用了类似的东西.

struct PIDList* 
getRecordForPID(struct PIDList* list, pid_t pid) {
    while(list = list->next)
        if (list->pid == pid)
            return list;

    return NULL;
}
Run Code Online (Sandbox Code Playgroud)

c compiler-construction compiler-warnings

43
推荐指数
3
解决办法
10万
查看次数

是否允许<button>显示:grid?

或者,更一般地说,是否有任何无法设计的元素display:grid

考虑:

button,
div {
  display: grid;
  grid-template-columns: 50px 50px;
}


/* Nevermind these, they're just for a consistent display */

button,
div {
  border: 0;
  background-color: gray;
  color: black;
  width: 100px;
  text-align: center;
  font-family: sans-serif;
  font-size: 14px;
  padding: 0;
}
Run Code Online (Sandbox Code Playgroud)
Button:
<button>
  <span>A</span>
  <span>B</span>
</button>
<br> 
Div:
<div>
  <span>A</span>
  <span>B</span>
</div>
Run Code Online (Sandbox Code Playgroud)

在JsFiddle上尝试一下

这是Firefox(61.0.1)中的结果:

两者看起来都一样

这是Chrome(68.0.3440.106)中的结果:

按钮有问题

Chrome似乎不喜欢我正在尝试使用display:grid按钮.这只是一个错误吗?或者是以某种方式打算?

css css-grid

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

从c/c ++中的文件读取最后n行

我看过很多帖子但没找到像我想要的东西.
我输错了:

ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ......  // may be this is EOF character
Run Code Online (Sandbox Code Playgroud)

进入无限循环.

我的算法:

  1. 转到文件末尾.
  2. 将指针的位置减1并逐个字符地读取.
  3. 退出,如果我们找到我们的10行或我们到达文件的开头.
  4. 现在我将扫描整个文件直到EOF并打印它们//未在代码中实现.

码:

#include<iostream>
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>

using namespace std;
int main()
{
    FILE *f1=fopen("input.txt","r");
    FILE *f2=fopen("output.txt","w");
    int i,j,pos;
        int count=0;
        char ch;
        int begin=ftell(f1);
        // GO TO END OF FILE
        fseek(f1,0,SEEK_END);
        int end = ftell(f1);
        pos=ftell(f1);

        while(count<10)
        {
            pos=ftell(f1);
            // FILE IS LESS THAN 10 LINES
            if(pos<begin)
                break;
            ch=fgetc(f1);
            if(ch=='\n')
                count++;
            fputc(ch,f2);
            fseek(f1,pos-1,end);
        }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

UPD 1:

改变了代码:它现在只有1个错误 - 如果输入有像这样的行

3enil
2enil …
Run Code Online (Sandbox Code Playgroud)

c++ file

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

为什么我不能从基类的实例访问受保护的成员?

假设我有这段代码:

class foo{
  protected:
  int a;
};

class bar : public foo {
  public:
  void copy_a_from_foo(foo& o){
    a = o.a; // Error
  }
  void copy_a_from_bar(bar& o){
    a = o.a; // OK
  }
};

int main(){
  bar x;
  foo y;
  bar z;
  x.copy_a_from_foo(y);
  x.copy_a_from_bar(z);
}
Run Code Online (Sandbox Code Playgroud)

这里class bar有没有问题访问受保护的成员a来自同一类的其他实例,但是当我尝试做同样与基类的一个实例foo,编译器给我的错误,说a是受保护的.标准对此有何看法?

错误是

prog.cpp: In member function 'void bar::copy_a_from_foo(foo&)':
prog.cpp:3:7: error: 'int foo::a' is protected
   int a;
       ^
prog.cpp:9:11: error: within this context
     a = o.a;
Run Code Online (Sandbox Code Playgroud)

PS:我看过这个问题 …

c++

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

如何参数化构造函数的参数个数?

我想在模板类构造函数中接受多个参数(此数字在模板参数中定义).我不能使用initializer_list,因为据我所知,我无法在编译时断言它的大小.

我尝试了什么

我的第一次尝试是使用std :: array作为参数:

template<size_t s>
class foo {
  int v[s];
public:
  foo(std::array<int, s>) {/*...*/}
};
Run Code Online (Sandbox Code Playgroud)

但是,这迫使我像这样初始化(即使构造函数不是这样explicit):

foo<4> a{{1,2,3,4}} // Two brackets.
Run Code Online (Sandbox Code Playgroud)

我认为可能有一些模板魔术(可变参数模板?),但我甚至无法弄清楚在构造函数中使用的正确语法.我不能递归地调用构造函数...可以吗?

我试图寻找构造函数的定义std::array(因为它不允许比数组大小更多的参数,只是我想要的),但我能找到的只是它有隐式构造函数.这是默认的构造函数吗?如果是这样,怎么做

std::array<int, 3> a = {1,2,3}
Run Code Online (Sandbox Code Playgroud)

工作?

可选奖励:为什么标准没有定义固定尺寸替代std::initializer_list?有点像std::static_initializer_list<T, N>.是否有计划在未来支持此类功能?它甚至需要吗?

c++ templates constructor c++11

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

依靠RVO最终功能

阅读C++编程语言(第4版),在"异常处理"一章中,有一个用于临时清理代码的示例帮助:

template<typename F>
struct Final_action {
    Final_action(F f): clean{f} {}
    ~Final_action() { clean(); }
    F clean;
};

template<class F>
Final_action<F> finally(F f)
{
    return Final_action<F>(f);
}
Run Code Online (Sandbox Code Playgroud)

它用得像

auto act1 = finally([&]{ delete p; });
Run Code Online (Sandbox Code Playgroud)

在声明act1的块的末尾运行lambda代码.

我认为这对Stroustrup在测试时起作用,因为返回值优化限制Final_action<>为单个实例 - 但是RVO不是可选的优化吗?如果在从finally返回时复制实例,则显然会~Final_action()为两个副本运行.换句话说,p被删除两次.

这种行为是否被标准中的某些东西阻止了,或者代码是否足够简单以便大多数编译器对其进行优化?

c++ c++11

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

这里发生了什么?我将结果分配给C++中的结果

你能告诉我这里发生了什么吗?为什么有可能呢?

std::make_unique<int>(1) = std::make_unique<int>(1);
Run Code Online (Sandbox Code Playgroud)

我以为这make_unique返回了一个r值......

编辑: 你能提供一些这种结构的有用例子吗?

c++ unique-ptr c++14

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

如何简化由多条贝塞尔曲线组成的路径?

我有一条由多个三次贝塞尔曲线组成的路径,我需要将其简化为更平滑的路径。

路径

这基本上就是 Inkscape 的 Simplify(Ctrl+L) 算法在多次应用时所做的事情。

在上面的示例中,我应用该simplify算法直到节点数变为< 10。但这不是强制性的,任何数量的节点/阈值度量都是可以接受的,只要它不是慢得难以忍受。

以下是本示例中的两条路径(采用 SVG 语法):

Original path (black):
m 14550,8173.5 c 0,5 -7,10 -16,10 -20,0 -44,24 -44,44 0,19 -16,21 -23,3 -2,-6 -8,-8 -13,-4 -5,4 -2,13 6,18 11,8 12,12 3,16 -7,2 -13,17 -13,33 0,16 -6,31 -12,33 -10,4 -9,8 2,16 13,10 13,12 0,22 -11,8 -12,12 -2,16 6,2 12,11 12,20 0,19 24,43 43,43 9,0 18,6 21,12 5,10 7,10 12,0 3,-6 19,-12 35,-12 16,0 29,-5 29,-10 0,-6 9,-10 20,-10 13,0 20,-7 20,-20 0,-11 5,-20 …
Run Code Online (Sandbox Code Playgroud)

geometry bezier

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

C++ 类型属性是继承的吗?

我认为这并不是真正的函数属性是否继承的重复?,因为我想知道类,而不是成员函数:

struct [[nodiscard]] error {};
struct critical_error : error {};

critical_error foo();

int main() {
   foo(); // no warning.
}
Run Code Online (Sandbox Code Playgroud)

看来[[nodiscard]]这里的属性没有被继承。所有类型属性都相同吗?

c++ c++11

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

捕获组中的负向后看

我正在尝试编写一些正则表达式,以使我可以对捕获组进行隐式查找,以便可以从电子邮件中提取可能的引用。我需要知道如何从某个角度看向第一个空白。如果找到一个数字,我不希望提取引用。

我已经达到如下所示。我有2个捕获组-'PreRef'和'Ref'。如果'PreRef'包含数字,我不希望找到'Ref'匹配项。到目前为止,我只检查冒号前面的字符是否为数字。

(?<PreRef>\S+)(?<![\d]):(?<Ref>\d{5})
Run Code Online (Sandbox Code Playgroud)

此处的“参考”匹配为12345:

This is a reference:12345
Run Code Online (Sandbox Code Playgroud)

但是不是这里(“引用”一词中有5):

This is not a ref5rence:12345
Run Code Online (Sandbox Code Playgroud)

regex regex-group regex-lookarounds

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