__is_constexpr(x)Linux内核的宏如何工作?它的目的是什么?什么时候介绍?为什么要介绍?
/*
* This returns a constant expression while determining if an argument is
* a constant expression, most importantly without evaluating the argument.
* Glory to Martin Uecker <Martin.Uecker@med.uni-goettingen.de>
*/
#define __is_constexpr(x) \
(sizeof(int) == sizeof(*(8 ? ((void *)((long)(x) * 0l)) : (int *)8)))
Run Code Online (Sandbox Code Playgroud)
有关解决相同问题的不同方法的讨论,请参阅:检测宏中的整数常量表达式
我一直试图了解如何导入函数以在 Rust 中进行测试几个小时,但没有成功。我有一个如下所示的项目结构:
\n.\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 Cargo.lock\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 Cargo.toml\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 src\n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 main.rs\n\xe2\x94\x82 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 funcs\n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 mod.rs\n\xe2\x94\x82 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 hello.rs\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 tests\n \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 test_hello.rs\nRun Code Online (Sandbox Code Playgroud)\nsrc/funcs/mod.rs:
pub mod hello;\nRun Code Online (Sandbox Code Playgroud)\nsrc/funcs/hello.rs:
pub fn hello() {\n println!("{}", "hello!");\n}\nRun Code Online (Sandbox Code Playgroud)\nsrc/main.rs:
mod funcs;\n\nfn main() {\n funcs::hello::hello(); // this works\n}\nRun Code Online (Sandbox Code Playgroud)\nsrc/tests/test_hello.rs
mod funcs; // this import does not work!\n\n#[test]\nfn add() {\n assert_eq!(2 + 2, 4);\n}\n\n#[test]\nfn hello_test() {\n assert_eq!(funcs::hello::hello(), "hello");\n}\nRun Code Online (Sandbox Code Playgroud)\n如何导入公共函数src以便它们可以在我的测试目录中使用?
从这个问题,我们从6.3.2.3p5(C11)知道,我们可以将任何整数转换为指针(即,它本身不是UB):
整数可以转换为任何指针类型。除非先前指定,否则结果是实现定义的,可能未正确对齐,可能未指向引用类型的实体,并且可能是陷阱表示。
然后,从6.5.9p6开始,我们有:
当且仅当两个都是空指针时两个指针比较相等,两个指针都是指向同一对象的指针(包括指向对象和它的开始处的子对象的指针)或函数,都是指向同一数组最后一个元素的指针对象,或者一个是指向一个数组对象末尾的指针,另一个是指向另一个数组对象的起点的指针,该数组对象恰好紧随地址空间中的第一个数组对象。
因此,似乎我们可以在没有UB的情况下应用相等运算符(与关系运算符不同)。考虑:
struct A;
int f(void) {
struct A * a = (struct A *) 1;
struct A * b = (struct A *) 1;
return a == b;
}
Run Code Online (Sandbox Code Playgroud)
假设的地址中没有A对象,则可能会争辩说应该返回,因为没有条件与上述条件匹配。a1f()false
如何反驳?即使没有对象,“指向同一对象的指针”是否也指向地址(无论如何,编译器也不知道)?我们是否应该简单地理解它是实现定义的,因为先前的结果已经是实现定义的?标准在哪里指定?
true正如预期的那样,所有主要的编译器都返回上述代码。
在 C++20 中,u8字符串文字基于char8_t类型。他们故意不再转换为char const*:
const char* str = u8"Hall\u00f6chen \u2603"; // no longer valid in C++20
Run Code Online (Sandbox Code Playgroud)
当然,迁移到 C++20 时的最终目标是完全采用新行为(在上面的示例中:更改 的类型str)。但是,由于 3rd 方库,这通常无法立即实现。
引入和“补救” 的提案char8_t预计并提到在 clang 和 gcc 中有-fno-char8_t切换回旧行为的标志(同时仍然能够享受其他 C++20 功能)。
第二个提案设定了 Microsoft 将遵循并添加类似标志的期望,但我找不到如何设置它(至少在 VS 2019 版本 16.4 中)。
那么有谁知道 MSVC 的等价物-fno-char8_t是什么?
什么是ve中execve意味着什么?
我读完了,man execve但没看到它意味着什么.我认为它可能是"矢量"但不确定.什么ve意思?
什么时候应该将noexcept属性添加到函数中?即编译器什么时候无法判断函数抛出异常?所有的东西都应该被标记吗?或者有什么方法可以区分吗?
我不喜欢过早优化,也不喜欢过早归因。我不知道有什么方法可以像noexcept优化时分析性能一样“分析”需求。
在评估必要时,请评论最常见的编译器,例如 MSVC、GCC 等。
对于整数x,x % (10 ** 9 + 7)并且x % (1e9 + 7)都给人一种几次迭代后不同的结果。为了重现这个结果,我分享了我的LeetCode #576解决方案。越界路径。如果我更改return ans % (10 ** 9 + 7)为return ans % (1e9 + 7)(意识到这花了我一个小时),我的解决方案将不会通过 94 个测试用例中的 5 个。
请注意,此解决方案比此处某个天才家伙提出的单行解决方案要长得多。然而,同样的问题,他的解决方案出现,如果我们改变% (10 ** 9 + 7)对% (1e9 + 7)。
我玩了一下 python 编译器,注意到 1e9 给出了一个浮点文字。所以在我看来,这种特性是由浮点运算的“怪异”引起的。但我仍然不明白小数点后的零如何导致差异。为什么会出现这种差异?
无需复制,可以在此处找到差异:https : //www.diffchecker.com/PyKQCElB
要重现,这是我的解决方案:
class Solution:
def findPaths(self, m: int, n: int, maxMove: int, startRow: int, startColumn: int) …Run Code Online (Sandbox Code Playgroud) [ 跟进检查boost :: log过滤器明确? ]
以下示例使用Boost Log中的普通记录器.它输出1,显示expensive()只调用一次.它是如何工作的?为什么expensive()不叫?
#include <iostream>
#include <boost/log/expressions.hpp>
#include <boost/log/trivial.hpp>
int count = 0;
int expensive()
{
return ++count;
}
int main()
{
boost::log::core::get()->set_filter(
boost::log::trivial::severity >= boost::log::trivial::warning
);
BOOST_LOG_TRIVIAL(error) << expensive();
BOOST_LOG_TRIVIAL(info) << expensive();
std::cout << count << '\n';
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:
[2018-05-21 14:33:47.327507] [0x00007eff37aa1740] [error] 1
1
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++ 强迫我在没有充分理由的情况下编写的额外行。