考虑以下代码:
#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)
$ 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) 我知道当我在 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) #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
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 …