我在许多帖子中都看到"在大多数情况下,数组名称会衰减为指针".
我可以知道在什么情况下/数组名称表达式不会衰减成指向其第一个元素的指针吗?
在下面的程序中,当mutable不使用时,程序将无法编译。
#include <iostream>
#include <queue>
#include <functional>
std::queue<std::function<void()>> q;
template<typename T, typename... Args>
void enqueue(T&& func, Args&&... args)
{
//q.emplace([=]() { // this fails
q.emplace([=]() mutable { //this works
func(std::forward<Args>(args)...);
});
}
int main()
{
auto f1 = [](int a, int b) { std::cout << a << b << "\n"; };
auto f2 = [](double a, double b) { std::cout << a << b << "\n";};
enqueue(f1, 10, 20);
enqueue(f2, 3.14, 2.14);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这是编译器错误
lmbfwd.cpp: In …Run Code Online (Sandbox Code Playgroud) 在std :: unordered_map上运行基于范围的for循环时,似乎循环变量的类型不使用引用类型:
std::unordered_map<int, int> map = { {0, 1}, {1, 2}, {2, 3} };
for(auto&[l, r] : map)
static_assert(std::is_same_v<decltype(r), int&>);
Run Code Online (Sandbox Code Playgroud)
MSVC 2017,gcc 8.2和clang 7.0.0都报告了一个失败的断言.将此反对到std :: vector,其中断言不会失败,正如人们所期望的那样:
std::vector<int> vec = { 1, 2, 3 };
for(auto& r : vec)
static_assert(std::is_same_v<decltype(r), int&>);
Run Code Online (Sandbox Code Playgroud)
然而,在MSVC 2017和gcc 8.2上,修改局部变量r的循环将具有可观察到的副作用:
#include <iostream>
#include <type_traits>
#include <unordered_map>
#include <vector>
int main() {
std::unordered_map<int, int> a = { {0, 1}, {1, 2}, {2, 3} };
for(auto[l, r] : a)
std::cout << l << "; " << …Run Code Online (Sandbox Code Playgroud) 这是我的代码,它是一个简单的排列代码块:
void arrange(char c[], int N, int start)
{
if (start == N)
{
print(c, N);
return;
}
for (int i = start; i < N; i++)
{
swap(c[start], c[i]);
arrange(c, N, i + 1);
swap(c[start], c[i]);
}
}
int main(int argc, char const *argv[])
{
char c[] = { 'A','B','C' };
int N = (sizeof(c) / sizeof(char));
arrange(c, N, 0);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它没有给出我期望的输出,我想调试这段代码。我想观察输入数组中的字符交换。但是当我调试时,输入数组无法扩展。
我尝试为我的类重载operator <<,以便在我执行std :: cout << obj时打印成员;
我知道这样做的方法是
std::ostream& operator<<(std::ostream& os, const T& obj)
{
// write obj to stream
return os;
}
Run Code Online (Sandbox Code Playgroud)
但是,我尝试使我的代码符合Google C++风格指南 https://google.github.io/styleguide/cppguide.html#Reference_Arguments
它表示不允许传递没有const的引用,除非按惯例需要它,例如swap().这个重载运算符<<与swap()在同一类别中吗?或者有办法做某事
std::ostream& operator<<(std::ostream* os, const T& obj)
^
Run Code Online (Sandbox Code Playgroud)
?或者不以非const引用作为输入的东西.
如果是这样,请教我如何做到这一点.谢谢.
请考虑以下代码:
#include <iostream>
class Test
{
public:
Test() : a{ 0 }
{}
void print() const
{
std::cout << "a : " << a << std::endl;
}
void operator->()
{
a = 5;
}
void operator++()
{
++a;
}
public:
int a;
};
int main()
{
Test a;
a.print();
// Increment operator
a.operator++(); // CORRECT
++a; // CORRECT
a.print();
// Indirection operator
a.operator->(); // CORRECT
a->; // INCORRECT
a.print();
}
Run Code Online (Sandbox Code Playgroud)
为什么对第二个->操作员的调用不正确?我知道这种用法->与一般用法不同,但这种用法是否被标准禁止?
我正在学习C++,所以请耐心等待我.我有一个std::valarray在其中有double元素,我认为这是一个二维矩阵.
class Matrix {
valarray<double> elems;
int r, c;
public:
/* type? operator[](int r) { return ? } */
//...
}
Run Code Online (Sandbox Code Playgroud)
我想重载operator[],以便我可以得到一行矩阵,然后,我想要m[r][c]访问运算符.
有没有什么办法让一排,为的序列double使用std::slice的valarray,所以,如果我改变的值,它也是在矩阵变化?
我在valarray中读过这个定义:
std::slice_array<T> operator[]( std::slice slicearr );
Run Code Online (Sandbox Code Playgroud)
我operator[]必须有std::slice_array<double>&返回类型?
谢谢.
我可以创建以下内容:
using Foo = struct { /*Implementation*/ };
template<class>
using Bar = Foo;
Run Code Online (Sandbox Code Playgroud)
但是,以下内容是不允许的:
template<class>
using Bar = struct { /*Implementation*/ };
Run Code Online (Sandbox Code Playgroud)
来自Clang的错误比GCC更有用,并指出:
错误:无法在类型别名模板中定义“(在file:line:column处的匿名结构)”
为何不允许使用第二个代码示例?
注意:
请说明第二个代码示例(如果允许)可能导致语言问题的所有示例。
该标准的任何引用也有帮助。
我正在开发 C 函数来使用以下方式关闭我的嵌入式 Linux 系统 (Ubuntu)。
#include <stdlib.h>
int main()
{
system("shutdown -P now");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这种方法安全吗?
如果没有,有没有更好更安全的方法可以执行相同的任务?
ISO C标准指出:
sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long)
Run Code Online (Sandbox Code Playgroud)
我在BIT Linux mint(19.1)上使用GCC-8,大小long int为8。
我正在使用使用GCC 7的应用程序,并且编译器是64位的。的大小long int为4。编译器或操作系统是否定义a的大小long int?