C++标准不需要整数类型的精确大小,这有时会导致非常意外的结果.然后,一些聪明的人引入了<cstdint>包含(可选)typedef 的头文件,例如int64_t具有正好64位宽度的类型.这不是想要的.
是否存在(可能是可选的)整数类型,其属性sizeof(mysterious_type) == 2适用于定义的任何系统?
推理:我正试图弄清楚系统的结束.为此,在审查了这个板上的许多问题之后,我虽然只想定义一个大小为2的整数类型,给它分配一个并检查endianess,如下所示:
enum endianess { LITTLE_ENDIAN, BIG_ENDIAN };
typedef utwowitdhtype test_t; // some unsigned type with width 2
endianess inspectSystem() {
static_assert(sizeof(twowitdhtype) == 2, "twowitdhtype is not size 2??!?!!");
test_t integral = 0x1;
return *reinterpret_cast<char*>(&integral) == 0 ? BIG_ENDIAN : LITTLE_ENDIAN;
}
Run Code Online (Sandbox Code Playgroud)
虽然这是为什么我会对这种类型感兴趣的原因,但找到这样的类型不是为了解决问题而是出于好奇.
我不知道这个片段有什么问题.我收到这个错误:
错误:成员函数'swap'不可行:'this'参数的类型为'const array',但函数未标记为const
#include <algorithm>
#include <memory>
#include <iostream>
#include <array>
struct MyClass {
std::array<float, 4> arr;
float carr[4];
std::array<float, 4> getArrElement() {
std::array<float, sizeof(carr) / sizeof(float)> out;
return out;
}
void fun() {
auto vec = { getArrElement(), getArrElement(), getArrElement() };
std::reverse(vec.begin(), vec.end()); // <-- error line here
}
};
int main()
{
MyClass obj;
obj.fun();
}
Run Code Online (Sandbox Code Playgroud)
getArrElement没有返回一个const数组.auto应该推断,std::initializer_list但我也认为没有坏处.
怎么了?
我有一个返回右值引用的函数.
auto function() -> int&& {
int x{10};
std::cout << x << std::endl; // to check the value of x
return std::move(x);
}
Run Code Online (Sandbox Code Playgroud)
现在,当我使用以下代码时:
std::cout << function() << std::endl;
std::vector<int> v;
v.push_back(function());
std::cout << v[0] << std::endl;
Run Code Online (Sandbox Code Playgroud)
这导致以下输出
10
0
Run Code Online (Sandbox Code Playgroud)
看起来即使function()返回一个rvalue-reference,vector也会推回一个默认的构造int.
有趣的是,如果我有这样的代码:
auto x = function();
v.push_back(std::move(x));
Run Code Online (Sandbox Code Playgroud)
这非常有效.
看来如果我只是从函数返回一个局部变量,那么RVO无论如何都会做一个复制省略.但是因为我正在做一个明确的std :: move()我正在绊倒RVO导致返回一个临时变量.
现在,在v.push_back(function())中我调用了vector :: push_back(T &&)函数,导致引用对temp变量的引用.在这种情况下,它总是倾向于0而不是垃圾(我猜这是因为优化打开).
但是,如果我尝试在局部变量中捕获function()的返回值,它可能会起作用,大概是因为在返回值的过程中创建的临时变量的值被复制到此局部变量.对于未定义的行为,这看起来并不幸运.
我在推理如何编写和设计几个应该处理特定文件格式的函数,这些函数可能有不同的实现和不同的版本,每个函数需要一种不同的方法来解码文件中的这些信息.
我像往常一样浏览标准库,我得到了一个std::function存在的余数,但问题是我无法弄清楚为什么我可能有兴趣使用std::function,在C和C++编程时的第一个规则之一是如果你不必引用你不一定要命名的东西,你可以获得未命名的数据结构和未命名/ lambda函数,但函数通常有一个名称,而lambda的类型仍然是实现根据我的记忆定义,那么需要std::function什么?
例如,在我的情况下,我正在考虑使用a map或an hash table(但所涉及的函数数量实际上很小,现在最多2-3个),其中一对由tag(表示文件格式的版本/实现)+ functions,我想知道为什么我不能只使用std::string和函数指针作为每对的2种类型; 我也无法理解为什么我们std::function在标准库中.
我的意思是,当你需要一个未命名的函数并且你需要一个对象来描述它的状态时呢?
在§7.5(C++ 14)的例子中,我发现:
enum {}; // ill-formed
但是,从技术上讲,我认为代码是有效的.enum {}是一个枚举说明符,因此,它是一个类型说明符,它是一个decl-specifier,因此,它是一个带有省略的init-declarator-list的简单声明.这被§7.5接受.请注意,对于未作用域的枚举,标识符是可选的.此外,clang编译此警告.
编辑
关于提到的答案the *decl-specifier-seq* shall introduce one or more names into the program, or shall redeclare a name introduced by a previous declaration,我在下面展示了一个编译的typedef声明,但是其decl-specifier-seq在声明中没有引入任何名称:
typedef class {} A;
Run Code Online (Sandbox Code Playgroud) 我有以下代码来检查某个值是否属于值列表。例如:contains({1,2,3},3). 几乎总是,我可以写一堆if-elses。该方法会对性能造成多大影响contains?有办法避免这种情况吗?
template<typename T1,typename T2>
std::enable_if_t<std::is_same<std::initializer_list<T2>,T1>::value, bool> contains(const T1 &container,const T2 &item)
{
return(std::find(container.begin(),container.end(),item)!=container.end());
}
Run Code Online (Sandbox Code Playgroud) 我正在编写一些像这样的C++代码:
#include <iostream>
using namespace std;
void swap(int *a, int *b)
{
int *c = NULL;
*c = *a;
*a = *b;
*b = *c;
}
int main()
{
int a, b;
cout << "a, b: ";
cin >> a;
cin >> b;
swap(&a, &b);
cout << a << b;
system("pause");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但在编译时,它说:"未处理的异常......访问违规读取......".我的代码出了什么问题?
#include <iostream>
using namespace std;
int main(){
int t;
long long int n,res,x;
scanf("%d",&t);
while(t--){
scanf("%lld",&n);
res=0;
for(int i=0;i<n;i++){
scanf("%lld",&x);
res^=x;
}
if(res==0)
printf("-1\n");
else
printf("%lld\n",res);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
this same program when i used cin and cout was timed out in hackerearth. But passed with scanf and printf.
我刚刚在一个情况下稳定了一个unique_pointer引用作为函数参数传递.所以我看一下它,发现代码实际上是在编译和运行.
为什么会这样?当你可以引用那个指针时,指针是如何唯一的?
这是我的例子:
class Foo{
public:
int bar{23};
};
void Bar(std::unique_ptr<Foo>& a_foo){
a_foo->bar = 42;
}
int main(int argc, char *argv[]){
auto foo{ std::make_unique<Foo>() };
Bar(foo);
std::cout << foo->bar << std::endl; //outputs 42
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我的程序应该要求用户添加一个单词,然后将其添加到string数组中,之后将打印出所有单词.但是,程序在while循环中崩溃,并且字符串不会添加到数组中.
#include <iostream>
int main()
{
int counter = 0;
bool isRunning = true;
std::string listofthings[]
{
};
while(isRunning)
{
char play;
std::string word;
std::cout << "Enter a word" << std::endl;
std::cin >> word;
listofthings[counter] = word;
counter++;
std::cout << "Word added! q to Quit, any other key to continue" << std::endl;
std::cin >> play;
if(play == 'q')
isRunning = false;
}
int listnr = sizeof(listofthings)/sizeof(listofthings[0]);
for(int i = 0; i < listnr; i++)
{
std::cout << listofthings[i] …Run Code Online (Sandbox Code Playgroud)