小编Wan*_*nze的帖子

使用 [&] 捕获子句调用 lambda 时,在 C++ 中使用当前范围之外的变量时,是什么导致了奇怪的行为?

考虑以下代码:

#include <iostream>
#include <functional>

std::function<void ()> f()
{
    int x = 666;
    return [&] { std::cout << x << std::endl; };
}

int main()
{
    f()();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

在 Ubuntu 18.04 仿生 (WSL) 上使用 GCC 7.5.0 进行编译:

无优化

$ g++ -o main -std=c++14 -Wall main.cpp
$ ./main
666
Run Code Online (Sandbox Code Playgroud)

-O1

$ g++ -o main -O1 -std=c++14 -Wall main.cpp
$ ./main
0
Run Code Online (Sandbox Code Playgroud)

-O2

$ g++ -o main -O2 -std=c++14 -Wall main.cpp
main.cpp: In function ‘int main()’:
main.cpp:7:31: warning: ‘x’ is …
Run Code Online (Sandbox Code Playgroud)

c++ lambda gcc compilation

10
推荐指数
1
解决办法
248
查看次数

pip 的 --user 选项在 Windows 上有什么用?

我知道当我在 Linux 上安装 Python 包时,我需要使用 的--user选项pip将包安装在我的主目录中,否则我将需要 root 权限。但是在windows上好像不管我是否使用这个选项,这个包都会一直安装在我的主目录(C:\Users\{username}\...)中,只是具体路径会略有不同。使用该--user选项,软件包将安装到c:\users\{username}\appdata\roaming\python\python37\site-packages,而没有此选项,软件包将安装到c:\users\{username}\appdata\local\programs\python\python37-32\lib\site-packages

C:\>pip3 install pyyaml
Collecting pyyaml
  Using cached https://files.pythonhosted.org/packages/45/19/53c041b8719eaf88ce1cdb51bea1c5a2844433e79c23a2a8aeeaa0e27fd8/PyYAML-5.1.1-cp37-cp37m-win32.whl
Installing collected packages: pyyaml
Successfully installed pyyaml-5.1.1

C:\>pip3 show pyyaml
Name: PyYAML
Version: 5.1.1
Summary: YAML parser and emitter for Python
Home-page: https://github.com/yaml/pyyaml
Author: Kirill Simonov
Author-email: xi@resolvent.net
License: MIT
Location: c:\users\{username}\appdata\local\programs\python\python37-32\lib\site-packages
Requires:
Required-by:

C:\>pip3 uninstall pyyaml
Uninstalling PyYAML-5.1.1:
  Would remove:
    c:\users\{username}\appdata\local\programs\python\python37-32\lib\site-packages\_yaml.cp37-win32.pyd
    c:\users\{username}\appdata\local\programs\python\python37-32\lib\site-packages\pyyaml-5.1.1.dist-info\*
    c:\users\{username}\appdata\local\programs\python\python37-32\lib\site-packages\yaml\*
Proceed (y/n)? y
  Successfully uninstalled PyYAML-5.1.1

C:\>pip3 install …
Run Code Online (Sandbox Code Playgroud)

python pip

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

MSVC 和 GCC 受 std::driven_from 约束的部分模板专业化编译方式不同

#include <concepts>

class Base {};

class Derived : public Base {};

// primary template
template <typename T>
    requires std::derived_from<T, Base>
struct C {
    static constexpr int value = 1;
};

// specialization
template <typename T>
    requires std::derived_from<T, Derived>
struct C<T> {
    static constexpr int value = 2;
};

static_assert(C<Derived>::value == 2);
Run Code Online (Sandbox Code Playgroud)

事实证明,MSVC 可以很好地编译代码 ( godbolt ),而 GCC 拒绝编译并显示错误消息 ( godbolt ):

错误:部分特化“struct C”不特化任何模板参数,并且不比主模板受到更多约束

我想知道根据语言标准,哪种行为是正确的,以及专门针对C++20 中C<T>所有T派生自Derived(而不是Base)(包括自身)的正确方法是什么。Derived

c++ language-lawyer c++20

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

有没有办法在GCC中禁用内联汇编程序?

I'm developing an online judge system for programming contests like LeetCode, Codeforces, etc. As for most programming contests, inline assembler is not allowed in C/C++, so I would like to add the same restriction to my system.

I would like to let GCC and G++ produce an error when compiling a C/C++ program containing inline assembler, so that any code containing inline assembler will result in compilation error.

Is there a way to achieve that? Should I pass some command …

c c++ gcc g++ inline-assembly

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

标签 统计

c++ ×3

gcc ×2

c ×1

c++20 ×1

compilation ×1

g++ ×1

inline-assembly ×1

lambda ×1

language-lawyer ×1

pip ×1

python ×1