我正在尝试使用 unicode 字符在终端中进行一些简单的框画。但是我注意到 wcout 不会为框绘图字符输出任何内容,甚至不会输出占位符。所以我决定写下面的程序,找出支持哪些unicode字符,发现wcout拒绝输出255以上的任何东西。有什么我必须做的事情才能使wcout正常工作?为什么无法访问任何扩展的 unicode 字符?
#include <wchar.h>
#include <locale>
#include <iostream>
using namespace std;
int main()
{
for (wchar_t c = 0; c < 0xFFFF; c++)
{
cout << "Iteration " << (int)c << endl;
wcout << c << endl << endl;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud) 这与我先前的问题有关,我问是否std::chrono::steady_clock::now应该这样noexcept.现在我知道我应该想知道这个函数如何报告错误?例如,Linux上此函数的常见实现使用
clock_gettime,它可以返回错误.
请考虑以下示例:
template <typename T>
struct A {
struct B {
int b;
};
struct C : B {
void f() {
b = 0;
}
};
};
Run Code Online (Sandbox Code Playgroud)
使用GCC 4.8.1进行编译会出现以下错误:
test.cc: In member function ‘void A<T>::C::f()’:
test.cc:9:11: error: ‘b’ was not declared in this scope
b = 0;
^
Run Code Online (Sandbox Code Playgroud)
但是,b是父类的成员B(我struct在示例中用于使所有内容都公开),如果我创建A非模板,则所有内容都会编译.
为什么编译器会出现此错误,如何避免错误?
考虑以下(人为)示例:
class A {
public:
template <typename T>
class C {};
};
class B : private A {
public:
using A::C;
};
int main() {
B::C<int> c;
}
Run Code Online (Sandbox Code Playgroud)
它与GCC和Clang成功编译,但Visual C++ 2010出现以下错误:
test.cpp(13):错误C2247:'A :: C'无法访问,因为'B'使用'private'继承'A'
这是Visual C++中的错误还是这段代码确实无效?
如果C不是模板,则代码将编译在所有编译器上.
如何获取在类体中定义为朋友的重载运算符的地址?
我尝试了以下内容
struct S {
friend bool operator==(S, S) { return true; }
};
int main() {
bool (*f)(S, S) = &operator==;
}
Run Code Online (Sandbox Code Playgroud)
但是gcc给出了一个错误
test.cc: In function ‘int main()’:
test.cc:6:30: error: ‘operator==’ not defined
bool (*f)(S, S) = &operator==;
^
Run Code Online (Sandbox Code Playgroud)
可以通过在(全局)命名空间中声明运算符来修复:
bool operator==(S, S);
Run Code Online (Sandbox Code Playgroud)
有没有办法在没有重新声明运营商的情况下获取地址?
根据C++标准的[time.cal.wd.overview]/1节:
weekday代表民事日历中的一周中的某一天.它通常持有值的范围0来6,通过相应周六至周日,但它可能会保持在这个范围之外的非负值.
同时算术运算执行模7运算,强制该范围内的结果[0, 6],例如
weekday wd(7);
// wd.ok() == false - wd is invalid
++wd; // wd == weekday(1)
// wd.ok() == true - wd silently becomes valid
Run Code Online (Sandbox Code Playgroud)
为什么会weekday出现这种奇特的行为,特别是为什么数值超出[0, 6]允许范围而不能通过算术运算保留?
我想使用,std::format但 Visual Studio 说std命名空间没有 member format。
这似乎是 C++20 的新功能。有没有办法让它可用?
我已经看到 std::format 是多么有用。但是每次我尝试将它包含在 C++20 中时,它都不起作用。它显然已经包含在其图书馆中,但没有出现,而且网上也没有任何信息。甚至 cppreference 也有它的示例,但它甚至不能在其在线编译器上运行。我附上了它的一个片段。
你们有没有人知道如何在没有超级复杂的 GitHub 导入库的情况下使其工作。我主要在 VisualStudio 上工作。
#include <iostream>
#include <format>
int main() {
std::cout << std::format("Hello {}!\n", "world");
}
Run Code Online (Sandbox Code Playgroud)
main.cpp:2:10: fatal error: No such file or directory
2 | #include <format>
| ^~~~~~~~
compilation terminated.
Run Code Online (Sandbox Code Playgroud) 我有一个包含许多格式化写入语句的程序,我正在使用 fmt 库。其中一些有很多字段,例如 100 个字段,如下所示:
fmt::print(file_stream, "{:15.5g}{:15.5g}{:15.5g}/*and so on*/", arg1, arg2, arg3/*same number of arguments*/);
Run Code Online (Sandbox Code Playgroud)
有没有一种简单的方法来截断字段,这样就不必将它们全部写出来?显然这个例子行不通,但它说明了这个想法:
fmt::print(file_stream, 100("{:15.5g"), arg1, arg2, arg3/*etc*/);
Run Code Online (Sandbox Code Playgroud) 在 中fmt/core.h,我注意到该函数使用带有给定谓词的count_named_args()模板函数。count
我发现版本的重载版本count很奇怪:
template <bool B1, bool B2, bool... Tail> constexpr auto count() -> size_t {
return (B1 ? 1 : 0) + count<B2, Tail...>();
}
Run Code Online (Sandbox Code Playgroud)
为什么我们需要使用模板参数bool B2来显式提取下一个布尔值,而不是直接使用参数包bool... Tail?
如果我删除这些bool B2内容,然后尝试编译:
template <bool B1, bool... Tail> constexpr auto count() -> size_t {
return (B1 ? 1 : 0) + count<Tail...>();
}
Run Code Online (Sandbox Code Playgroud)
static_assert(count<false>() == 0);
static_assert(count<true, false>() == 1);
Run Code Online (Sandbox Code Playgroud)
当参数数量减少到最后一个时,它会给出一个错误,因为确定重载是不明确的:
size_t count<false,>(void) noexcept
size_t count<false>(void) noexcept
Run Code Online (Sandbox Code Playgroud)