C++23 引入了对隐式参数和函数本身的std::move_only_function支持。constl/r-value-referencethisnoexcept(true/false)
旧的std::function缺乏这些超载。
#include <functional>\n\nint main(){\n std::function<void()>([]noexcept(true){}); // okay \xe2\x9c\x93\n std::function<void()>([]noexcept(false){}); // okay \xe2\x9c\x93\n std::function<void()noexcept>([]noexcept(true){}); // not supported\n std::function<void()noexcept>([]noexcept(false){}); // not supported\n\n std::move_only_function<void()>([]noexcept(true){}); // okay \xe2\x9c\x93\n std::move_only_function<void()>([]noexcept(false){}); // okay \xe2\x9c\x93\n std::move_only_function<void()noexcept>([]noexcept(true){}); // okay \xe2\x9c\x93\n std::move_only_function<void()noexcept>([]noexcept(false){}); // fails \xe2\x9c\x93\n\n std::function<void()>([i=0]{}); // okay \xe2\x9c\x93\n std::function<void()>([i=0]mutable{}); // okay \xe2\x9c\x93\n std::function<void()const>([i=0]{}); // not supported\n std::function<void()const>([i=0]mutable{}); // not supported\n\n std::move_only_function<void()>([i=0]{}); // okay \xe2\x9c\x93\n std::move_only_function<void()>([i=0]mutable{}); // okay \xe2\x9c\x93\n std::move_only_function<void()const>([i=0]{}); // okay \xe2\x9c\x93\n std::move_only_function<void()const>([i=0]mutable{}); // fails \xe2\x9c\x93\n}\n …Run Code Online (Sandbox Code Playgroud) I\xc2\xb4m 尝试运行我在 github 上获得的项目: https: //github.com/ranahanocka/MeshCNN
\n首先,我使用 ranahanocka 项目中的 .yml 文件在 anaconda 中创建了一个环境,然后出现了与 CUDA 安装相关的错误。
\n为了解决这个问题,我按照本教程安装 CUDA,并消除了该错误:
\nconda install -c anaconda cudatoolkit=10.1\nRun Code Online (Sandbox Code Playgroud)\n但现在我收到这些错误:
\n\n在train.py:
在base_option.py:
并在__init__.py:
我不知道如何解决它,我已经阅读了几个论坛,但没有解决方案对我有帮助。我尝试过安装这个:
\npip install torch===1.9.0+cu111 torchvision===0.10.0+cu111 torchaudio===0.9.0 -f https://download.pytorch.org/whl/cu111/torch_stable.html\nRun Code Online (Sandbox Code Playgroud)\n但在下载过程中我收到以下错误:
\n\n\n我不知道会发生什么,此外,我不太了解 CUDA 的工作原理及其依赖关系,所以如果我不提供必要的信息,我很抱歉。
\n好吧,在调试时,例如当我在简单的字符串声明上设置断点时
string a;
Run Code Online (Sandbox Code Playgroud)
然后按 f11(步入)我的调试器步入xstring文件,但我不想要它。我希望它只进入我的代码。这适用于 C# 项目,但不适用于 C++ 项目。
我如何在 Visual Studio 中为 C++ 项目定义它:

我启用了“仅我的代码”,但我不知道该怎么办。我只是不想让它介入我的文件。
如果我编译这个 Qt“hello world”:
#include <QtWidgets/QApplication>
#include <QtWidgets/QPushButton>
int main( int argc, char **argv ) {
QApplication a( argc, argv );
QPushButton hello( "Hello world!" );
hello.resize( 100, 30 );
hello.show();
return a.exec();
}
Run Code Online (Sandbox Code Playgroud)
在 Debian Stable(使用系统 Qt 库)和 GCC 12.2(从源代码安装)上
$ g++ -fsanitize=address hello.cpp -L /usr/lib/x86_64-linux-gnu -lQt5Core -lQt5Gui -lQt5Widgets -I /usr/include/x86_64-linux-gnu/qt5 -fPIC
Run Code Online (Sandbox Code Playgroud)
当我关闭应用程序时,这会导致检测到泄漏:
$ ./a.out
=================================================================
==3563910==ERROR: LeakSanitizer: detected memory leaks
Direct leak of 6656 byte(s) in 26 object(s) allocated from:
#0 0x7fea3b4801af in __interceptor_malloc ../../../../gcc-12.2.0/libsanitizer/asan/asan_malloc_linux.cpp:69
#1 0x7fea3693c704 (/lib/x86_64-linux-gnu/libfontconfig.so.1+0x20704)
Indirect …Run Code Online (Sandbox Code Playgroud) 如果我没记错的话,用户定义的文字的参数在编译时总是已知的。在 C++20 中,您可以使用强制函数在编译时执行,consteval从而throw生成编译时错误。
#include <limits>\n\nconsteval int operator""_int(unsigned long long int v) {\n if (std::numeric_limits<int>::max() < v) {\n throw "out of range";\n }\n return static_cast<int>(v);\n}\n\nint main() {\n return 1\'000\'000\'000\'000_int;\n}\nRun Code Online (Sandbox Code Playgroud)\n$ g++ -std=c++20 main.cpp\nmain.cpp: In function \xe2\x80\x98int main()\xe2\x80\x99:\nmain.cpp:11:12: in \xe2\x80\x98constexpr\xe2\x80\x99 expansion of \xe2\x80\x98operator""_int(1000000000000)\xe2\x80\x99\nmain.cpp:5:9: error: expression \xe2\x80\x98<throw-expression>\xe2\x80\x99 is not a constant expression\n 5 | throw "out of range";\n | ^~~~~~~~~~~~~~~~~~~~\nRun Code Online (Sandbox Code Playgroud)\n根据我的经验,编译时错误通常比运行时错误更可取。
\n如果必须在定义中调用其他不是 的函数constexpr,那么consteval显然不是一个选项。但除了这个例子,我想不出有什么理由不使用consteval.
还有其他原因不将用户定义的文字标记为 吗consteval …
c++ constant-expression user-defined-literals c++20 consteval
该函数std::char_traits::copy的实现libc++如下:
template <class _CharT>
inline _LIBCPP_CONSTEXPR_AFTER_CXX17
_CharT*
char_traits<_CharT>::copy(char_type* __s1, const char_type* __s2, size_t __n)
{
if (!__libcpp_is_constant_evaluated()) {
_LIBCPP_ASSERT(__s2 < __s1 || __s2 >= __s1+__n, "char_traits::copy overlapped range");
}
char_type* __r = __s1;
for (; __n; --__n, ++__s1, ++__s2)
assign(*__s1, *__s2);
return __r;
}
Run Code Online (Sandbox Code Playgroud)
情况_LIBCPP_ASSERT似乎倒退了。难道不应该吗
__s1 < __s2 || __s1 >= __s2+__n
Run Code Online (Sandbox Code Playgroud)
我缺少什么?
以下情况:
namespace abc{
inline namespace x{
int f() { return 5; }
}
inline namespace y{
int f() { return 6; }
}
int f() { return 7; }
void g(){
x::f(); // okay
y::f(); // okay
f(); // error: ambiguous!
abc::f(); // error: ambiguous!
}
}
Run Code Online (Sandbox Code Playgroud)
GCC 和 clang 同意,这是 GCC 错误消息:
<source>: In function 'void abc::g()':
<source>:16:10: error: call of overloaded 'f()' is ambiguous
16 | f(); // error: ambiguous!
| ~^~
<source>:10:9: note: candidate: 'int abc::f()'
10 …Run Code Online (Sandbox Code Playgroud) struct A{
constexpr operator bool()const{ return true; }
};
int main(){
auto f = [](auto v){ if constexpr(v){} };
A a;
f(a);
}
Run Code Online (Sandbox Code Playgroud)
clang 6 接受代码,GCC 8 拒绝它:
$ g++ -std=c++17 main.cpp
main.cpp: In lambda function:
main.cpp:6:37: error: 'v' is not a constant expression
auto f = [](auto v){ if constexpr(v){} };
^
Run Code Online (Sandbox Code Playgroud)
谁是正确的,为什么?
当我根据每个引用获取参数时,都拒绝代码:
struct A{
constexpr operator bool()const{ return true; }
};
int main(){
auto f = [](auto& v){ if constexpr(v){} };
constexpr A a;
f(a);
}
Run Code Online (Sandbox Code Playgroud)
用clang …
我有一个带有 SpringBoot2 和 Junit5 的应用程序,现在我正在尝试进行测试。我有一个名为 OrderService 的类,如下所示:
@Component
public class OrderService {
@Value("#{'${food.requires.box}'.split(',')}")
private List<String> foodRequiresBox;
@Value("#{'${properties.prioritization}'.split(',')}")
private List<String> prioritizationProperties;
@Value("${further.distance}")
private Integer slotMeterRange;
@Value("${slot.meters.long}")
private Double slotMetersLong;
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,该类有许多 @Value 注释,用于从 application.properties 文件中提取值。
在 POM 文件中,我有以下依赖项:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.1.0</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>RELEASE</version>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<version>2.0.5.RELEASE</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
在test/resources文件夹中,我有包含以下信息的 application.properties 文件:
properties.prioritization:vip,food
food.requires.box:pizza,cake,flamingo
further.distance:2
slot.meters.long:0.5
Run Code Online (Sandbox Code Playgroud)
测试文件如下所示:
properties.prioritization:vip,food
food.requires.box:pizza,cake,flamingo
further.distance:2
slot.meters.long:0.5
Run Code Online (Sandbox Code Playgroud)
但测试在尝试使用foodRequiresBox时会抛出 …
我想在编译之前替换源代码文件中的a = statement;字符串。// something else这对应于一个预处理器#define,但是要替换的字符串不是一个简单的标识符。所以我想让 CMake 在编译之前执行此操作。
我的文件结构是:
main.cpp
CMakeLists.txt
build/
Run Code Online (Sandbox Code Playgroud)
的不可变内容main.cpp:
main.cpp
CMakeLists.txt
build/
Run Code Online (Sandbox Code Playgroud)
当前内容CMakeLists.txt:
cmake_minimum_required(VERSION 3.0)
project(main)
file(READ main.cpp TEXT)
string(REPLACE "foo" "bar" TEXT "${TEXT}")
file(WRITE main.cpp "${TEXT}")
add_executable(main main.cpp)
Run Code Online (Sandbox Code Playgroud)
在build/我调用cmake配置和构建我的项目:
#include <iostream>
int main() {
std::cout << "foo\n";
}
Run Code Online (Sandbox Code Playgroud)
目前,在main.cpp文件中的配置过程中,该字符串foo被替换为bar. 然后将更改后的文件编译为可执行文件。
我希望 CMake 在构建期间读取main.cpp、替换字符串、写入文件CMAKE_BINARY_DIR,然后将这个新文件编译为可执行文件add_executable。这可能吗?
不应使用外部工具,因为我无法控制编译该项目的系统。configure_file似乎不起作用,因为这里不能替换任意字符串。