在我的第一个例子中,我使用了两个位域int64_t.当我编译并获得类的大小时,我得到8.
class Test
{
int64_t first : 40;
int64_t second : 24;
};
int main()
{
std::cout << sizeof(Test); // 8
}
Run Code Online (Sandbox Code Playgroud)
但是,当我将第二个bitfeild更改int32_t为类的大小时,会增加到16:
class Test
{
int64_t first : 40;
int32_t second : 24;
};
int main()
{
std::cout << sizeof(Test); // 16
}
Run Code Online (Sandbox Code Playgroud)
这种情况发生在GCC 5.3.0和MSVC 2015上.但为什么呢?
在下面的C++代码中,我可以显式调用析构函数而不是构造函数.这是为什么?不明确的ctor调用与dtor案件更具表现力和统一性吗?
class X { };
int main() {
X* x = (X*)::operator new(sizeof(X));
new (x) X; // option #1: OK
x->X(); // option #2: ERROR
x->~X();
::operator delete(x);
}
Run Code Online (Sandbox Code Playgroud) c++ constructor destructor placement-new explicit-destructor-call
这是代码:
struct Biology
{
Biology() { cout << "Biology CTOR" << endl; }
};
struct Human : Biology
{
Human() { cout << "Human CTOR" << endl; }
};
struct Animal : virtual Biology
{
Animal() { cout << "Animal CTOR" << endl; }
};
struct Centaur : Human, Animal
{
Centaur() { cout << "Centaur CTOR" << endl; }
};
int main()
{
Centaur c;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
此代码打印:
Biology CTOR
Biology CTOR
Human CTOR
Animal CTOR
Centaur CTOR …Run Code Online (Sandbox Code Playgroud) c++ inheritance constructor multiple-inheritance virtual-inheritance
是否可以通过const引用返回默认参数的值,如以下示例所示:
https://coliru.stacked-crooked.com/a/ff76e060a007723b
#include <string>
const std::string& foo(const std::string& s = std::string(""))
{
return s;
}
int main()
{
const std::string& s1 = foo();
std::string s2 = foo();
const std::string& s3 = foo("s");
std::string s4 = foo("s");
}
Run Code Online (Sandbox Code Playgroud) c++ object-lifetime language-lawyer default-arguments reference-binding
我刚刚在一些代码中发现了这个:
class Foo {
[...]
private:
virtual void Bar() = 0;
[...]
}
Run Code Online (Sandbox Code Playgroud)
这有什么用途吗?
(我试图将一些代码从VS移植到G ++,这引起了我的注意)
我正在考虑在实时应用程序中使用虚拟继承.使用虚拟继承是否会产生类似于调用虚函数的性能影响?有问题的对象只会在启动时创建,但我担心层次结构中的所有函数是否都将通过vtable调度,或者只是来自虚拟基类的函数.
重要的澄清:一些评论者似乎认为我是从工会复制的.仔细查看memcpy,它从一个普通旧的地址复制uint32_t,它不包含在一个联合中.此外,我正在(通过memcpy)复制到一个联盟的特定成员(u.a16或者&u.x_in_a_union,不是直接复制到整个联盟本身(&u)
C++对工会非常严格 - 只有在成员写入的最后一个成员时才应该从成员中读取:
9.5 Unions [class.union] [[c ++ 11]]在一个联合中,最多一个非静态数据成员可以随时处于活动状态,即最多一个非静态的值数据成员可以随时存储在联合中.
(当然,编译器不会跟踪哪个成员是活动的.由开发人员来确保他们自己跟踪它)
更新:以下代码块是主要问题,直接反映问题标题中的文本.如果这段代码没问题,我会对其他类型进行跟进,但我现在意识到第一块代码本身很有趣.
#include <cstdint>
uint32_t x = 0x12345678;
union {
double whatever;
uint32_t x_in_a_union; // same type as x
} u;
u.whatever = 3.14;
u.x_in_a_union = x; // surely this is OK, despite involving the inactive member?
std::cout << u.x_in_a_union;
u.whatever = 3.14; // make the double 'active' again
memcpy(&u.x_in_a_union, &x); // same types, so should be …Run Code Online (Sandbox Code Playgroud) n3797说:
§7.1.6.4/14:
使用占位符类型的返回类型声明的函数不应是虚拟的(10.3).
因此,以下程序格式错误:
struct s
{
virtual auto foo()
{
}
};
Run Code Online (Sandbox Code Playgroud)
虚拟
允许对虚函数进行返回类型推导是可能的,但这会使重写检查和vtable布局复杂化,因此似乎最好禁止这种情况.
任何人都可以提供进一步的理由或给出一个与上述引用一致的好(代码)示例吗?
前几天有人曾问过为什么有些东西会与clang编译,而不是与gcc编译.我直观地理解了发生了什么,并且能够帮助这个人,但它让我想知道 - 根据标准,哪个编译器是正确的?这是代码的简化版本:
#include <iostream>
#include <string>
class foo
{
public:
foo(const std::string& x):
name(x)
{ }
foo& operator()(const std::string& x)
{
std::cout << name << ": " << x << std::endl;
return (*this);
}
std::string name;
};
int main()
{
std::string x = "foo";
foo(x)("bar")("baz");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
使用clang ++可以很好地编译,但是g ++会出现以下错误:
runme.cpp: In function ‘int main()’:
runme.cpp:21:11: error: conflicting declaration ‘foo x’
foo(x)("bar")("baz");
^
runme.cpp:20:17: error: ‘x’ has a previous declaration as ‘std::string x’
std::string x = …Run Code Online (Sandbox Code Playgroud) c++ ×10
constructor ×2
auto ×1
bit-fields ×1
c++11 ×1
c++14 ×1
clang++ ×1
destructor ×1
g++ ×1
inheritance ×1
memory-model ×1
mutex ×1
overriding ×1
performance ×1
private ×1
real-time ×1
syntax ×1
unions ×1
vtable ×1