注意:这个问题使用C++20,我使用的是Visual Studio 2022 (v17.2.2)。
我想创建一个模板函数包装器以允许我使用 std::format 样式日志记录。包装函数最终将执行一些其他与格式无关的操作,这些操作在这里并不重要。
请参考下面的代码。请注意,Log1()工作正常,但使用起来感觉很笨拙。 Log2()没问题,但使用std::vformat()会丢失日志格式字符串的编译时检查。
我真正想做的是Log3()。问题是 Visual Studio (v17.2.2) 不喜欢这样。
有什么方法可以让它工作(没有宏)?
#include <iostream>
#include <format>
#include <string_view>
// works fine: usage is clunky
auto Log1(std::string_view sv)
{
std::cout << sv;
}
// works, but fmt string is checked at runtime - not compile time
template<typename... Args>
auto Log2(std::string_view fmt, Args&&... args)
{
std::cout << std::vformat(fmt, std::make_format_args(args...));
}
// this doesn't work
template<typename... Args>
auto Log3(std::string_view fmt, Args&&... args)
{ …Run Code Online (Sandbox Code Playgroud) 众所周知,没有进入 C++20 格式库的一件事是打印到标准输出或通用文件流的函数。我们已承诺std::print()在 C++23 中满足这一需求,但这并不能解决临时问题。
有什么选择可以解决这个问题?
std::print() 将在 C++23 中添加。
我想知道是否std::print()是线程安全的,因为没有数据竞争
它是否存在文本交错问题,例如,如果我在线程 1 中存在:
std::print("The quick brown fox ")
std::print("jump over the lazy dog \n")
Run Code Online (Sandbox Code Playgroud)
和线程 2:
std::print("She sells ")
std::print("seashells by the seashore \n")
Run Code Online (Sandbox Code Playgroud)
它会以疯狂的顺序打印吗,就像这样:
She sells The quick brown fox seashells by the seashore \n
jump over the lazy dog \n
Run Code Online (Sandbox Code Playgroud)
我想这两个问题的答案都是肯定的,与 的行为相匹配std::cout,但是任何人都可以将我与标准所说的联系起来吗?
我有一个 LaTeX 长表,其标题不适合一行,例如
\begin{longtable}{lrrr}
\caption{This is a very long caption that does not fit into one line}
...
\end{longtable}
Run Code Online (Sandbox Code Playgroud)
生成的 PDF 如下所示:
Figure 1: This is a very long caption that does not fit
into one line
Run Code Online (Sandbox Code Playgroud)
我希望标题的第二行与标题的开头(而不是文本“图 1”)对齐,使其看起来像
Figure 1: This is a very long caption that does not fit
into one line
Run Code Online (Sandbox Code Playgroud)
或者至少使标题文本居中。我怎样才能做到这一点?
在C++中,可以将临时绑定到const引用:
struct A {};
int main() {
const A& a = A();
}
Run Code Online (Sandbox Code Playgroud)
有没有办法为某个特定的A类禁用它,这样就不可能将这个类的临时值绑定到const引用?
我注意到,std::chrono::steady_clock::now有noexcept在文档中说明符cplusplus.com.但是,我在最新的C++ 11草案中没有找到任何相关规定(遗憾的是我没有该标准的副本).
这是cplusplus.com文档中的错误还是应该std::chrono::steady_clock::now有说明noexcept符?
0.8804418在Fortran中打印常量
program hello
print *, 0.8804418
end program hello
Run Code Online (Sandbox Code Playgroud)
给出0.880441785了C版本
#include <stdio.h>
int main() {
printf("%.10g", 0.8804418);
}
Run Code Online (Sandbox Code Playgroud)
0.8804418在同一系统上打印(带有gfortran和gcc的Linux x86-64).为什么输出不同?请注意,提高精度printf并不会改变输出.
给定这个 Rust 程序,它打印1.23456:
use std::fmt;
struct Foo(f64);
impl fmt::Display for Foo {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(formatter, "{}", self.0)
}
}
fn main() {
println!("{:.2}", Foo(1.23456));
}
Run Code Online (Sandbox Code Playgroud)
对函数体进行最简单的更改fmt以进行打印1.23?一般来说,是否有一种简单的方法可以在函数内部使用fmt打印对象的代码设置的相同格式选项?我知道格式化程序对象有几种访问格式化选项的方法,但是有没有一种简单的方法可以始终获得通过调用获得的相同结果println!("{:.2}", Foo(1.23456).0);?
我很高兴标准库能够获得一个to_string功能,但现在我处于WTF模式.首先,为什么不是这个模板,其次如果它不是返回值的模板那么为什么在世界上他们没有to_u16string()和to_u32string()功能.
我知道有提升词法演员,但我很想知道是否有一种标准的方法来获得我想要的东西而无需手动编写这些功能?
编辑:使事情更糟糕的提升1.46也不喜欢u16string :(
boost::lexical_cast<u16string>(22.44);
Run Code Online (Sandbox Code Playgroud)
在抛出'boost :: exception_detail :: clone_impl的实例后终止调用
'what():错误的词法转换:源类型值无法解释为目标
假设我想定义一组函数,每个函数有4个重载,第一个重载采用单个参数类型int32_t,第二个采用int64_t第三个 - uint32_t第四个 - uint64_t.对于每个函数,所有重载都具有相同的实现,因此我可以定义一个函数模板:
template <typename T>
void f(T t) {
// ...
}
Run Code Online (Sandbox Code Playgroud)
然而,这与四次重载不同,因为现在我有一个可用于实例化的每个(整数)类型的单独函数f.但是,实现细节f可能不适用于其他整数类型.为了解决这个问题,我可以将函数模板包装在四个重载函数中:
template <typename T>
void f_impl(T t) {
// ...
}
void f(int32_t value) { f_impl(value); }
void f(int64_t value) { f_impl(value); }
void f(uint32_t value) { f_impl(value); }
void f(uint64_t value) { f_impl(value); }
Run Code Online (Sandbox Code Playgroud)
它工作但每个函数需要大量代码(4个函数重载+ 1个函数模板).有没有办法简化这个?
为了澄清,这是不可取的直接使用模板,因为它没有任何意义(实施的原因或其他方式)有其特比其他类型的int32_t,int64_t,uint32_t和uint64_t.
我已经尝试过使用std::enable_if了,这个例子最好地说明了它的问题:
#include <type_traits>
#include <iostream>
template <typename …Run Code Online (Sandbox Code Playgroud) 我已阅读有关 Go fmt 包的文档。尽管如此,我还是不明白 Print、Fprint、Sprint、Printf、Fprintf 和 Sprintf 之间的区别。有人可以用外行的术语向我解释吗?