我想在 macOS 中启用 的调试版本,所以我在[libC++ 的调试版本]libc++
中定义,但无法链接. 我猜我的系统中只有release版本,那么如何在macOS中获取debug版本_LIBCPP_DEBUG=1
cxx_build_flags
libc++
libc++
libc++
Undefined symbols for architecture x86_64:
"std::__1::__libcpp_db::__decrementable(void const*) const", referenced from:
void std::__1::__nth_element<std::__1::__debug_less<std::__1::__less<float, float> >&, std::__1::__wrap_iter<float*> >(std::__1::__wrap_iter<float*>, std::__1::__wrap_iter<float*>, std::__1::__wrap_iter<float*>, std::__1::__debug_less<std::__1::__less<float, float> >&)
Run Code Online (Sandbox Code Playgroud) 在 C++20 中 - 如何使用户定义的类型与 兼容std::format
?
例如,假设我有一个名为 的类型Point
:
struct Point {
int x;
int y;
};
Run Code Online (Sandbox Code Playgroud)
其operator<<
定义:
inline std::ostream&
operator<<(std::ostream& o, Point pt)
{ return o << "[" << pt.x << << ", " << pt.y << "]"; }
Run Code Online (Sandbox Code Playgroud)
那么下面的程序会输出Hello [3, 4]!
吗?
int main() {
Point pt{3,4};
std::cout << std::format("Hello {}!\n", pt);
}
Run Code Online (Sandbox Code Playgroud)
如果是 - 为什么以及如何?
如果不是 - 我必须在 to 的定义中添加什么Point
才能使其工作?
我知道技术答案是:因为标准是这样说的。
但我对动机感到困惑:
我什么也看不到“库”中违约的<=>
:它可能会返回在技术上定义了一些类型std
,但它在某种意义上是一种“假库”类型的编译器必须知道关于它,因为它必须能够默认operator <=>
与auto
返回类型(更不用说好的编译器中的错误消息指定了<compare>
所以很明显这里有一个语言<=>库链接)。
所以我知道有些库功能可能需要我包含,<compare>
但我不明白为什么默认设置<=>
需要我包含该头文件,因为编译器无论如何都必须知道制作<=>
.
注意:我知道大多数时候其他一些标准头文件会包含<compare>
,这是一个关于语言/库设计的问题,而不是 C++ 强迫我在没有充分理由的情况下编写的额外行。
使用NamedPipeServerStream
读取和NamedPipeClientStream
写入数据,当两个进程由同一用户运行时,一切正常,但如果我由不同的用户运行客户端进程,我会得到Permission denied /tmp/CoreFxPipe_my_pipe
. 管道“文件”确实没有其他用户的写入权限。
PipeOptions
有一个 member CurrentUserOnly
,但我没有设置它(我什至尝试None
显式设置)。如何允许其他用户写入管道?
我尝试编写一个迭代器,通过索引迭代容器。AIt
和 aconst It
都允许更改容器的内容。AConst_it
和 aconst Const_it
都禁止更改容器的内容。
之后,我尝试span<T>
在容器上写一个。对于一个类型T
不是常量,这两个const span<T>
和span<T>
允许改变所述容器的内容物。双方const span<const T>
并span<const T>
禁止改变容器的内容。
代码无法编译,因为:
// *this is const within a const method
// But It<self_type> requires a non-const *this here.
// So the code does not compile
It<self_type> begin() const { return It<self_type>(*this, 0); }
Run Code Online (Sandbox Code Playgroud)
如果我让构造函数It
接受一个const
容器,它看起来不对,因为迭代器可以修改容器的内容。
如果我去掉方法的const,那么对于非const 类型T,aconst span<T>
不能修改容器。
It
从继承Const_it
到允许隐式转换It
到Const_it
模板实例化时。 …
当运行 git-am 来应用我从邮件列表中保存到 mbox 文件中的补丁系列时,如果 mbox 还包含求职信(也称为 PATCH [0/N]),它会抱怨如下:
\n\n128 git \xe2\x80\xa6 am --3way ~/patches/sample.mbox\nPatch is empty.\nWhen you have resolved this problem, run "git am --continue".\nIf you prefer to skip this patch, run "git am --skip" instead.\nTo restore the original branch and stop patching, run "git am --abort".\n
Run Code Online (Sandbox Code Playgroud)\n\n确实,求职信是一个空补丁,因为其中不包含补丁分隔符,但我认为git am
足够聪明,可以注意到它并完全跳过它。这并不是什么大麻烦,因为我可以毫无问题地跳过从 mutt 将这个补丁添加到 mbox 中,但有时补丁系列到达邮件列表时有些混乱,而且我不能只是将整个线程复制到同一个 mbox 中。 \n或者我可以手动跳过补丁,它将顺利应用。
当它确实是一封求职信时,有什么方法可以指示 git am 跳过补丁号 0 吗?
\n我使用 GCC 10 标志构建-fsanitize=address
并且可以很好地运行程序,但是如果我在 Valgrind 下运行它会显示 Asan 错误:
==477229== Memcheck, a memory error detector
==477229== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==477229== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==477229== Command: ./a.out
==477229==
==477229==ASan runtime does not come first in initial library list; you should either link runtime to your application or manually preload it with LD_PRELOAD.
==477229==
==477229== HEAP SUMMARY:
==477229== in use at exit: 0 bytes in 0 …
Run Code Online (Sandbox Code Playgroud) 我有一个 C 程序,它通过 (Linux) 管道从另一个程序接收数据。如果在写入任何数据之前关闭管道,我希望程序的行为有所不同。
执行此操作的自然方法是尝试从管道中读取并检查是否得到EOF
,但这会消耗管道中的一些数据(如果有可用的数据),并且(据我所知)无法将数据“放回” “在管道中。
我想要检查管道是否为空的程序部分离我处理数据的地方很远,所以我宁愿不必处理保存第一次读取的数据直到那时。
有没有什么方法可以检查管道是否为空(read
将返回EOF
)而不消耗任何数据(如果管道不为空)?
注意:如果管道尚未写入或关闭,我确实希望阻止此操作。
以下是我的程序的两个变体
struct customer{
char fullname[35];
double payment;
};
int main()
{
customer alex{"Alex", 15};
return 0;
}
Run Code Online (Sandbox Code Playgroud)
struct customer{
char fullname[35];
double payment;
};
int main()
{
customer* alex = new customer {"Alex", 15};
return 0;
}
Run Code Online (Sandbox Code Playgroud)
第一个工作正常,但第二个引发error:could not convert '{"Alex", 15}' from 'brace-enclosed initializer list' to 'customer'
. 有什么问题?
阅读[algorithms.parallel.exec]p2上最新的 C++20 草案,我发现了 C++17 中没有的一段:
\n\n\n\n\n如果某个对象被元素访问函数修改,则该算法将不会对该对象执行其他非同步访问。修改元素访问函数是那些被指定为修改对象的函数。[注意:例如,
\nswap
、++
、--
、@=
和 赋值修改对象。对于赋值和@=
运算符,仅修改左侧参数。\xe2\x80\x94尾注]
这究竟保证了什么?为什么添加它?
\n\n例如,这是否保证“并行”算法在算法的整个执行过程中不会将迭代器递增两次?(或者,如果是的话,它将以同步/顺序的方式)?
\n