小编ytj*_*ytj的帖子

可以在constexpr构造函数(C++ 14)中修改非静态成员变量吗?

struct A {
    int a = 0;
    constexpr A() { a = 1; }
};

constexpr bool f() {
    constexpr A a;
    static_assert(a.a == 1, ""); // L1: OK
    return a.a == 1;
}
static_assert(f(), ""); // L2: Error, can not modify A::a in constexpr
Run Code Online (Sandbox Code Playgroud)

如果我删除L2,则此代码编译.如果我添加L2,编译器抱怨"在常量表达式中不允许修改const限定类型的对象'const int'".我不是语言律师,所以我不确定这是否属实.但是,如果是,为什么编译器没有抱怨任何关于L1的东西,因为它也称为A()为constexpr?这是一个铿锵的错误吗?或者我错过了什么?

参考:http://en.cppreference.com/w/cpp/language/constexpr

顺便说一句,如果我改变"constexpr A a;" 到"A a;" (删除constexpr关键字),L1无法编译,这是期望的.但是,编译器不再抱怨L2了.

关于此的在线编译器URL:http://goo.gl/AoTzYx

c++ constexpr c++14

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

是否可以在zsh中自动分页输出?

有时我们在终端运行一个命令,输出太大,我们忘了把"| less"放到最后.所以我想知道当zsh中的输出太大时可以分页输出吗?

我尝试使用python和less实现此功能:

#!/usr/bin/env python3
termHeight = 25
import sys
from subprocess import Popen, PIPE
p = Popen(['unbuffer'] + sys.argv[1:], stdin=PIPE, stdout=PIPE)
lines = []
for count in range(termHeight):
    line = p.stdout.readline()
    if not line:
        break
    print(line.decode('utf8'), end='')
    lines += [line]
if line:
    q = Popen(['less', '-Mr'], stdin=PIPE)
    q.stdin.writelines(lines)
    while True:
        line = p.stdout.readline()
        if not line:
            break
        q.stdin.write(line)
    q.communicate()
Run Code Online (Sandbox Code Playgroud)

让我们将这个python脚本保存到p.py. 因此,当我们运行"python p.py some commands"之类的"python p.py ls --help"时,如果输出超过25行,则此脚本将使用less来显示输出.

问题是我无法从用户那里获得输入.这意味着此解决方案根本不适用于交互式程序.

zsh

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

标签 统计

c++ ×1

c++14 ×1

constexpr ×1

zsh ×1