Here is a little program (live on godbolt):
static void UnusedDeclaration ();
static void UnusedDefinition () {}
static void Declaration ();
decltype (Declaration ())* global;
Run Code Online (Sandbox Code Playgroud)
Ideally I would expect the following warnings, if I compile it with clang, -Wunused:
UnusedDeclaration (): It is an unused function declaration with internal linkage. So I should get a warning.UnusedDefinition (): It is an unused function definition with internal linkage. So I should get a warning.Declaration () …为什么在+使用运算符语法调用一元时会收到错误消息?如果我用函数语法调用它,就可以了。现场演示。
template <int size>
struct Buffer { char buf[size]; };
template <class T>
struct Wrapper { void operator+() {} };
Wrapper<Buffer<-5>> a;
void f1() { +a; } // error: Buffer<-5>::buf has negative size
void f2() { a.operator+(); } // OK
Run Code Online (Sandbox Code Playgroud) 假设我们有两种类型,它们具有相同的表示形式(相同的成员变量和基类,顺序相同)。它们之间是否有效(即不是 UB)reinterpret_cast?例如,reinterpret_cast从Mary到有效吗Ashley&?如果这两种类型是多态的怎么办?
struct Mary {
int m1;
char m2;
};
struct Ashley {
int a1;
char a2;
};
int TryTwins ()
{
Mary mary = {};
Ashley& ashley = reinterpret_cast<Ashley&> (mary);
ashley.a1 = 1;
ashley.a2 = 2;
return mary.m1 + mary.m2;
}
Run Code Online (Sandbox Code Playgroud)
如果我们知道源类型以目标类型的成员变量开头,那么我们将对象的开头强制转换为另一种类型会怎样?例如,这有效吗(即不是 UB)?
struct Locomotive {
int engine;
char pantograph;
};
struct Train {
int engine;
char pantograph;
int* wagon1;
int** wagon2;
int*** wagon3;
};
int TryTrain ()
{
Train train …Run Code Online (Sandbox Code Playgroud) c++ strict-aliasing undefined-behavior language-lawyer reinterpret-cast
所以我在 Visual Studio 2015 中使用 SFML 编写了一个简单的图形蛇游戏,它在我的主计算机上完美运行。我想我应该在我的笔记本电脑上尝试一下。运行程序时,它给了我这个错误: 系统错误:程序无法启动,因为您的计算机缺少 MSVCP140D.DLL。尝试重新安装程序来解决这个问题 所以我在我的电脑中搜索它并找到它所以我将它复制到我的笔记本电脑上,然后我再次收到另一个错误: 应用程序错误:应用程序无法正确启动(0xc000007b)。单击确定关闭应用程序。 我尝试重新安装 Microsoft Visual C++ Redistributable,但仍然无法正常工作。(顺便说一句,这不是代码问题,我已经正确安装了 SFML 并使用了它的库和 bin 没有任何问题)。你的帮助对我来说意义重大。谢谢!这是我的代码:
//
GraphicalLoopSnakeGame.cpp :
Defines the entry point for
the console application.
//
#include "stdafx.h"
#include <SFML/Graphics.hpp>
#include <time.h>
using namespace sf;
int N = 30, M = 20;
int size = 16;
int w = size*N;
int h = size*M;
int dir, num = 4;
struct Snake
{
int x, y;
} s[100];
struct Fruit
{
int x, y;
} …Run Code Online (Sandbox Code Playgroud) 假设我们有这样的类模板Wrapper:
template <class T>
struct Wrapper { T wrapped; };
Run Code Online (Sandbox Code Playgroud)
什么类型的reinterpret_castaType和 a之间是安全的Wrapper<Type>?没有任何?标准布局?全部?
假设我们创建了这些 (Type和Wrapper<Type>)之一的对象,并通过另一个读取和写入该对象。示例(在 Godbolt.org 上直播):
void F1() {
std::stringstream ss;
ss << "Hello";
reinterpret_cast<Wrapper<std::stringstream>&>(ss).wrapped << " world";
}
void F2() {
Wrapper<std::stringstream> ss;
ss.wrapped << "Hello";
reinterpret_cast<std::stringstream&>(ss) << " world";
}
Run Code Online (Sandbox Code Playgroud)
阅读这个答案的评论,这个领域在标准中似乎不是很明确。我认为所有编译器都会生成一个按预期工作的代码(即一种类型的值可以转换为另一种类型),但标准目前可能无法保证这一点。如果没有,问题就出现了:标准能否保证这些强制转换的定义明确的行为,或者在这种情况下保证任何事情是不可能/不切实际的?
因为我很确定,这些演员表实际上会起作用。
c++ strict-aliasing undefined-behavior language-lawyer reinterpret-cast
C++20 标准说(参见[expr.delete])
如果删除表达式的操作数的值为空指针值,则未指定是否将如上所述调用释放函数。
cppreference.com 说(请参阅删除表达式)
如果表达式计算结果为空指针值,则不会调用析构函数,并且可能会或可能不会调用释放函数(未指定),但保证默认释放函数在传递空指针时不执行任何操作。
如果删除表达式的操作数为 null,为什么编译器会调用释放函数?
如果可以保证不会调用释放函数,则规则会更简单,但我认为标准允许这样做是有充分理由的。
提案:我认为delete ptr;应该指定为与此等效:
if (ptr) {
// call dtor.
// call deallocation function
}
Run Code Online (Sandbox Code Playgroud)
如果ptr已知为空或不为空,我们就会进行相应的优化。释放函数会假设它们的参数不为空,因此不会!= nullptr发生双重检查。
我认为这个更简单。
这基于GCC/G ++,通常在Ubuntu上.
这是我做过的示例程序:
#include <iostream>
using namespace std;
int main()
{
std::string c = "Test";
cout << c;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
上面的代码工作正常.
但我有两个问题,我不太明白......
编写字符串声明std:string也可以正常工作.有什么不同.
如果我:在一个类中使用它来声明一个私有变量,我会收到一个错误错误:'std'没有命名一个类型.此声明的示例:
Run Code Online (Sandbox Code Playgroud)class KType { private: std:string N; };
有人可以解释这些问题吗?非常感谢!
我做了以下 3 个类:
struct Parent1
{
virtual void f()
{
cout << "\nParent1::f";
}
};
struct Parent2
{
virtual void g()
{
cout << "\nParent2::g";
}
virtual void z()
{
cout << "\nParent2::z";
}
};
struct Child : public Parent1, public Parent2
{
virtual void h()
{
cout << "\nChild::h";
}
};
Run Code Online (Sandbox Code Playgroud)
在 main 中,当我调用 的函数z时Parent2,它会调用h子类的函数。为什么会这样?以下是main函数:
int main()
{
Child obj;
Parent2 * p2 = (Parent2*)(Parent1*)&obj;
p2->z();
return 0;
}
Run Code Online (Sandbox Code Playgroud) c++ ×9
clang ×1
dll ×1
inheritance ×1
pointers ×1
standards ×1
string ×1
system-error ×1
templates ×1