从Stroustrup的的C++的基础,他提供了一个纯面向对象语言(第4页).
class complex { double re, im; /* … */ };
complex a[ ] = { {1,2}, {3,4} };
Run Code Online (Sandbox Code Playgroud)
他认为a纯粹的面向对象语言是在堆上分配的,而a内存布局如下:

可能的大小是3*sizeof(参考)+ 3*sizeof(heap_overhead)+ 4*sizeof(double).假设引用是一个单词而堆开销是两个单词,我们得到的可能大小为19个单词,与C++的8个单词进行比较.这种内存开销伴随着分配和间接访问元素的运行时开销.间接访问内存通常会导致缓存利用率问题并限制可ROM性.
我注意到最上面的引用没有堆开销(白色矩形).
我想这是一种普遍现象,而不是纯粹的OO示例语言.
但我找不到任何参考(我承认这不是一个搜索引擎友好的问题).
谢谢你的回答.但是,我忘记发布我的原始猜测.(道歉,这是我的错.)
实际上,我也认为因为a它本身可能被分配在堆栈或其他地方,它本身不会有堆开销.但后来,我注意到BS也说:
将其与"纯面向对象语言"中更典型的布局进行比较,其中
each user-defined object is allocated separately on the heap通过引用访问...
在第4页.
所以,我认为他已经将实现限制为仅限堆(或无堆栈).
(当然,也许我正在读这句话太多了.)
C ++ 17对基类的聚合初始化很棒,但是当基数仅用于提供某些功能(因此没有数据成员)时,它很冗长。
这是最小的示例:
#include <cstddef>
struct base_pod
{
// functions like friend compare operator
};
template<typename T, std::size_t N>
struct der_pod : public base_pod
{
T k[N];
};
int main()
{
der_pod<int, 2> dp {{}, {3, 3} };
}
Run Code Online (Sandbox Code Playgroud)
如上面的示例所示,我必须提供empty {},否则将发生编译错误。现场演示。如果我忽略它:
prog.cc:15:28: error: initializer for aggregate with no elements requires explicit braces
der_pod<int, 2> dp{3, 3};
^
prog.cc:15:31: warning: suggest braces around initialization of subobject [-Wmissing-braces]
der_pod<int, 2> dp{3, 3};
^
{}
1 warning and 1 …Run Code Online (Sandbox Code Playgroud) 我正在使用bitset并提高我的代码的性能我想将其更改为动态bitset,但在阅读了与此相关的一些帖子之后,我仍然不知道定义代码的方法.
所以我附上了我的代码,我想知道你们中是否有人可以帮助我给我一些关于我应该修改什么以及如何修改的想法.
提前致谢 :)
// Program that converts a number from decimal to binary and show the positions
// where the bit of the number in binary contains 1
#include <bitset>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
unsigned long long int dec;
bitset<5000> binaryNumber;
bitset<5000> mask;
mask = 0x1;
cout << "Write a number in decimal: ";
cin >> dec;
// Conversion from decimal to binary
int x;
for (x = 0; x < …Run Code Online (Sandbox Code Playgroud) 这个问题是为了确认我理解这个概念,并对使用方式和可能的优化方面采取专家意见.
我想了解"放置新",以下是我想出的程序......
#include <iostream>
#include <new>
class A {
int *_a;
public:
A(int v) {std::cout<<"A c'tor clalled\n";_a= new int(v);}
~A() {std::cout<<"A d'tor clalled\n"; delete(_a);}
void testFunction() {std::cout<<"I am a test function &_a = "<<_a<<" a = "<<*_a<<"\n";}
};
int main()
{
A *obj1 = new A(21);
std::cout<<"Object allocated at "<<obj1<<std::endl;
obj1->~A();
std::cout<<"Object allocated at "<<obj1<<std::endl;
obj1->testFunction();
A *obj2 = new(obj1) A(22);
obj1->testFunction();
obj2->testFunction();
delete(obj1);// Is it really needed now? Here it will delete both objects.. so this is not the …Run Code Online (Sandbox Code Playgroud) 我正在学习如何编程,但我无法理解的一件事是 preconditions 和postconditions。
调用函数之前的 if 语句是否被视为前提条件,或者在大多数语言中是否有单独的更有效的方法?
我也在努力寻找任何我可以用我目前的编程知识理解的先决条件的例子,如果有人能证明一个简单的例子,那么我真的很感激(任何语言都可以)
programming-languages design-by-contract preconditions post-conditions
我在下面阅读了以下材料:
https://www.wikiwand.com/en/One_Definition_Rule
http://en.cppreference.com/w/cpp/language/definition
但是,仍然无法弄清楚为什么它是一个定义规则而不是一个声明规则?
我认为声明是定义的一个子集,因此一个定义规则就足够了.
struct do_nothing
{
template <class T>
void operator()(T*) {}
};
int
main()
{
int i = 0;
std::unique_ptr<int, do_nothing> p1(&i);
std::unique_ptr<int> p2;
static_assert(!std::is_assignable<decltype(p2), decltype(p1)>::value, ""); // note ! here.
}
Run Code Online (Sandbox Code Playgroud)
如果表达式
std::declval<T>() = std::declval<U>()在未评估的上下文中格式良好,则提供成员常量值等于true.否则,值为false.从与任一类型无关的上下文执行访问检查.
template<class T>
typename std::add_rvalue_reference<T>::type declval() noexcept;
Run Code Online (Sandbox Code Playgroud)
返回类型是
T&&除非T(可能是cv-qualified)void,在这种情况下返回类型是T.
我们来看看MoveAssignOnly:
struct MoveAssignOnly {
MoveAssignOnly &operator=(MoveAssignOnly &) = delete;
MoveAssignOnly &operator=(MoveAssignOnly &&) = default;
};
int main()
{
static_assert(
not std::is_assignable<MoveAssignOnly, MoveAssignOnly>::value, "");
}
Run Code Online (Sandbox Code Playgroud)
现场演示: …
我的问题是Linux内核是否包含libc.so.6?在谷歌搜索并浏览不同的链接之后,我们发现libc.so.6,它不是Linux内核的一部分,因为内核在内核空间中实现了自己使用的相同库.libc.so.6是一个用户空间库.但是,问题仍然存在,如果libc.so.6从"/ lib"中删除,它会崩溃,因为Linux的所有基本应用程序崩溃了.
所以,基本问题留在:
libc.so.6吗?如果是,那么使用这种实施方式?libc.so.6?它仅由Linux发行版提供吗?例如,邪恶是 autoload,
一些博客/线程将使用
(require 'evil)
(evil-mode 1)
Run Code Online (Sandbox Code Playgroud)
启用邪恶模式。
AFAIK,Elisp 是 lisp2,evil-mode也是一个函数,所以不需要setq.
(defadvice evil-mode (after start-evil activate)
"Enable Evil in Fundamental mode."
(if evil-mode
(progn
(when (eq (default-value 'major-mode) 'fundamental-mode)
;; changed back by `evil-local-mode'
(setq-default major-mode 'turn-on-evil-mode))
(ad-enable-regexp "^evil")
(ad-activate-regexp "^evil")
(with-no-warnings (evil-esc-mode 1)))
(when (eq (default-value 'major-mode) 'turn-on-evil-mode)
(setq-default major-mode 'fundamental-mode))
(ad-disable-regexp "^evil")
(ad-update-regexp "^evil")
(with-no-warnings (evil-esc-mode -1))))
Run Code Online (Sandbox Code Playgroud)
但?因为autoload,
(evil-mode 1)
Run Code Online (Sandbox Code Playgroud)
没有require,它也可以正常工作(至少对我而言)
(以下如有不对的地方,请指正,谢谢)
我的问题是require如果需要的包是自动加载的,我什么时候必须使用
我使用ostreambuf_iterator,如下所示:
在c ++ 17之前,
template< class CharT, class Traits = std::char_traits<CharT> >
class ostreambuf_iterator : public std::iterator<std::output_iterator_tag,
void, void, void, void>
Run Code Online (Sandbox Code Playgroud)
要求我们确定std :: iterator的每个参数类型,因此,对于std::ostreambuf_iterator,void并不是那么糟糕.
我们知道, 在c ++ 17中不推荐使用std :: iterator.因此,迭代器应该在自己的主体中键入它们的成员类型,例如:
Member type Definition
---------------------------------------------
value_type void
difference_type void
pointer void
reference void
iterator_category std::output_iterator_tag
Run Code Online (Sandbox Code Playgroud)
题:
为什么这些void类型仍然是typedefed?我想,我想
Member type Definition
---------------------------------------------
iterator_category std::output_iterator_tag
Run Code Online (Sandbox Code Playgroud)
足够.毕竟,弃用的动机std::iterator只是为了简化标准(库).所以应该有一些我无法弄清楚的原因.
另一方面,将其定义为空白可以防止错误,例如:
Run Code Online (Sandbox Code Playgroud)typename Iter::value_type v = *it; //useless with an output iterator if …