在C ++中进行调试的有用打印内容是
std::cout << __LINE__ << std::endl;
Run Code Online (Sandbox Code Playgroud)
当然,您可以简单地打印带有行号的字符串,例如:
std::cout << "this is line 54" << std::endl;
Run Code Online (Sandbox Code Playgroud)
但是当您四处移动时,它不会一直更改行号。Python中是否有任何等效的宏?
I'm reading this book on C++ and there's a code example that should behave differently: it should throw an exception or error (sorry for the poor terminology here) anyway it just shouldn't work or so the book says. (the book is rather new so I think its up to date). But in my case the code executes itself and I get the "Exception caught" message.
The author uses a different compiler (WxDev). (I'm using Visual Studio 2019)
#include<exception>
void generate_char_exception() …Run Code Online (Sandbox Code Playgroud) 我的编译器对很多隐式转换感到很温暖:
有些我确实理解,比如
implicit conversion changes signedness: 'int' to 'std::vector::size_type' (aka 'unsigned long')`
Run Code Online (Sandbox Code Playgroud)
当我做myvector.resize(myInt) 。
其他比较晦涩难懂的,比如
implicit conversion loses integer precision: 'std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::size_type' (aka 'unsigned long') to 'int'
Run Code Online (Sandbox Code Playgroud)
当我这样做时myInt=myString.size(),或者
implicit conversion changes signedness: 'int' to 'std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::size_type' (aka 'unsigned long')
Run Code Online (Sandbox Code Playgroud)
当我打电话时myString[myInt]
在所有情况下,我确实理解为什么我收到这些消息(int-uint 等),但是它们在程序中的使用是明确的。如果我不更改变量类型来删除这些警告,我会面临什么风险?我以前的编译器没有警告我任何东西,所以当我更换电脑时,我突然收到了几十个警告。
代码:
let x = Some(3);
if x == Some(3) {
println!("if case");
}
if let Some(3) = x {
println!("if let case");
}
Run Code Online (Sandbox Code Playgroud)
结果:
if case
if let case
Run Code Online (Sandbox Code Playgroud)
为什么 Rust 程序员使用“if let”?
What is difference between int main() and signed main().
signed main()
{
....
}
int main()
{
....
}
Run Code Online (Sandbox Code Playgroud) int main()
{
int x = -2;
cout << (1<<x) << endl;
cout << (1<<-2) << endl;
}
Run Code Online (Sandbox Code Playgroud)
在这里(1<<x)打印1073741824(如何计算)
而(1<<-2)输出垃圾值。
为什么这两个返回不同的答案?
我刚刚从 Saar Raz 的演示视频中了解到以下限制:
template <typename T>
concept C = sizeof(T) > 1;
template <typename T>
concept D = sizeof(T) > 1 && sizeof(T) >= 4;
Run Code Online (Sandbox Code Playgroud)
对重载不明确,因为分别sizeof(T) > 1出现在C和的原子约束D不等价。
他们不是因为标准说[templ.constr]:
两个原子约束是相同的,如果它们是由相同的形成表达[...]
关键是表达式是斜体,指的是语法术语,定义为[expr.comma]:
表达:
赋值表达式
表达式,赋值表达式
我不明白为什么原子约束需要涉及赋值。为什么会这样?
我必须承认,上面的代码最好通过概念细化来编写,但我直觉地认为这种编写方式也是正确的。
新发布的草案在[expr.prim.req]/6 中提到:
如果将模板参数替换为需求总是会导致替换失败,则程序格式错误;无需诊断。[ 示例:
Run Code Online (Sandbox Code Playgroud)template<typename T> concept C = requires { new int[-(int)sizeof(T)]; // ill-formed, no diagnostic required };— 结束示例 ]
但是为什么我们不能保证诊断总是失败,而不是跳过诊断?
我正在尝试创建一个可从 C 调用的 Rust 库:
use std::os::raw::{c_int};
type OnDataCallback = unsafe extern "C" fn(data: *mut u8, len: usize) -> c_int;
static mut onDataCallback_: OnDataCallback = std::ptr::null();
#[no_mangle]
pub extern "C" fn registerOnDataCallback(
data: *const u8, len: usize,
cb: Option<OnDataCallback>) -> c_int
{
onDataCallback_ = cb.unwrap();
return 0;
}
#[no_mangle]
pub extern "C" fn doSomething()
{
unsafe{onDataCallback_(mut "hello world" , 100)};
}
Run Code Online (Sandbox Code Playgroud)
但我得到:
--> interface.rs:5:46
|
5 | static mut onDataCallback_: OnDataCallback = std::ptr::null();
| ^^^^^^^^^^^^^^^^ expected fn pointer, found *-ptr …Run Code Online (Sandbox Code Playgroud) std::string_view使用临时初始化a是一个常见的错误std::string。
using namespace std::literals;
std::string_view sv1 = "foo" ; // good
std::string_view sv2 = "bar"s; // bad: "foo"s will expire
std::cout << sv1 << "\n" // outputs foo
<< sv2 << "\n"; // undefined behavior
Run Code Online (Sandbox Code Playgroud)
这是因为"bar"s,临时表达式std::string在完整表达式的末尾被销毁了。
但是"foo"sv呢?
std::string_view sv3 = "baz"sv;
Run Code Online (Sandbox Code Playgroud)
当然这应该起作用,因为后缀sv否则是没有用的。但是,这与之"baz"s有何根本区别?换句话说,为什么引入的字符串"baz"sv不过期?
c++ ×7
c++-concepts ×2
c++20 ×2
rust ×2
bit-shift ×1
c ×1
c++17 ×1
exception ×1
python ×1
require ×1
signed ×1
string-view ×1
templates ×1
truncation ×1