We can have anonymous namespaces (namespace with no name). They are directly usable in the same program and are used for declaring unique identifiers. It is said that it avoids making global static variable.My question is if it is not static then why default value is zero? As these question has been asked before but it is not clear at all C++ anonymous namespace: Variables initialized to 0?
我们可以有匿名命名空间(没有名字的命名空间)。 它们可以在同一个程序中直接使用,并用于声明唯一标识符。 据说它避免了使全局静态变量。我的问题是,如果它不是静态的,那么为什么默认值为零? 由于这些问题之前已经被问到过,但在所有C ++匿名命名空间中都不清楚:变量初始化为0? #include<iostream> using namespace std; namespace { int x; // Here x should have garbage value according to definition
I would like to make an array of integers via the malloc method. I want this array to be global and be used anywhere in my program. I put code in a header file that looked like this: static int *pieces; Then I have a function that fills it with numbers that I want in there. The function is in a namespace and the namespace is implemented in its own .cpp file. However, I import the header fil
我想通过malloc方法创建一个整数数组。 我希望这个数组是全局的,并可以在我的程序中的任何地方使用。 我把代码放在一个如下所示的头文件中: static int *pieces; 然后我有一个函数,用它填充我想要的数字。 该函数位于命名空间中,命名空间在其自己的.cpp文件中实现。 但是,我将头文件导入到main.c中,并从创建数组的名称空间调用该函数,如下所示: pieces = malloc(sizeof(int) * 128); 但是当我尝试访问main中的数
Which is way to declare a string const is more recommended? Is to declare the global variable at file scope. To declare it global to the class. The variable will be used only in the class member functions. I am tending to feel 2 is better since it is specific to the class member functions only. A.cpp --------------------- static const std::string hello_str = "Hello"; void A::print() {
哪种方法来声明一个字符串const更值得推荐? 是在文件范围声明全局变量。 向班级宣布它是全球性的。 该变量将仅用于类成员函数中。 我倾向于认为2更好,因为它只针对集体成员职能。 A.cpp --------------------- static const std::string hello_str = "Hello"; void A::print() { std::cout << hello_str; } (要么) A.h --------------------- class A{ public: static const std::string hello_str;
I am trying to design header only library, which unfortunately needs to have global static variable (either in class or in namespace). Is there any way or preferred solution to have global static variable while maintaining header only design? The code is here There are a couple of options. The first thing that came to my mind was that C++ allows static data members of class templates to be
我试图设计仅头文件库,不幸的是需要有全局静态变量(无论是在类中还是在命名空间中)。 有没有什么方法或首选解决方案具有全局静态变量,同时仅维护头部设计? 代码在这里 有几个选项。 我首先想到的是,C ++允许在多个翻译单元中定义类模板的静态数据成员: template<class T> struct dummy { static int my_global; }; template<class T> int dummy<T>::my_global; inline int& my_global(
I have a function that is declared and defined in a header file. This is a problem all by itself. When that function is not inlined, every translation unit that uses that header gets a copy of the function, and when they are linked together there are duplicated. I "fixed" that by making the function inline, but I'm afraid that this is a fragile solution because as far as I know,
我有一个在头文件中声明和定义的函数。 这本身就是一个问题。 如果该函数未内联,则使用该标头的每个翻译单元都会获得该函数的一个副本,并且当它们链接在一起时会有重复。 我“固定”,通过内联函数,但恐怕这是一个脆弱的解决方案,因为据我所知,编译器不保证内联,即使您指定“内联”关键字。 如果不是这样,请纠正我。 无论如何,真正的问题是,这个函数中的静态变量会发生什么? 最终有多少份? 我想你在这里错过了一
I ran into a question as follows: We have a Code on Weighted, Acyclic Graph G(V, E) with positive and negative edges. we change the weight of this graph with following code, to give a G without negative edge (G') . if V={1,2...,n} and G_ij be a weight of edge i to edge j. Change_weight(G) for i=i to n for j=1 to n c_i=min c_ij for all j if c_i < 0 c_ij =
我碰到一个问题如下: 我们有一个具有正边和负边的加权非循环图G(V, E)的代码。 我们改变该曲线图的重量与下面的代码,得到G无负边缘(G') 如果V={1,2...,n}并且G_ij是边i到边j的权重。 Change_weight(G) for i=i to n for j=1 to n c_i=min c_ij for all j if c_i < 0 c_ij = c_ij-c_i for all j c_ki = c_ki+c_i for all k 我们有两个公理: 1)G中每两个顶点之间
I know that in C++11 we can now use using to write type alias, like typedef s: typedef int MyInt; Is, from what I understand, equivalent to: using MyInt = int; And that new syntax emerged from the effort to have a way to express " template typedef ": template< class T > using MyType = AnotherType< T, MyAllocatorType >; But, with the first two non-template examples, are
我知道在C ++ 11中,我们现在可以using用于写入类型别名,如typedef s: typedef int MyInt; 据我所知,相当于: using MyInt = int; 这种新的语法来源于努力去表达“ template typedef ”: template< class T > using MyType = AnotherType< T, MyAllocatorType >; 但是,对于前两个非模板示例,标准中是否还有其他细微差别? 例如, typedef以“弱”方式进行别名。 也就是说,它不会创建新的类型,而只是一个
In unmanaged C/C++ code, what are the best practices to detect memory leaks? And coding guidelines to avoid? (As if it's that simple ;) We have used a bit of a silly way in the past: having a counter increment for every memory allocation call and decrement while freeing. At the end of the program, the counter value should be zero. I know this is not a great way and there are a few catc
在非托管C / C ++代码中,检测内存泄漏的最佳实践是什么? 和编码准则,以避免? (就好像它很简单;) 过去我们使用了一些愚蠢的方法:每次分配内存时调用计数器增量,并在释放时减少。 在程序结束时,计数器值应该为零。 我知道这不是一个好方法,并且有几个捕获点。 (例如,如果您释放由平台API调用分配的内存,则您的分配计数将不会与您的空闲计数完全匹配。当然,当调用分配内存的API调用时,我们会增加计数器。)
It has happened to many people, and it happened to me. I got stuck playing with compile time strings in C++. I decided to take the apparently unusable approach: using template <char...> classes. This is what I came up with, it is very common, nothing special, and also it does not work. template <char... chars> class string { public: static constexpr const char value[] = {ch
它发生在很多人身上,而且发生在我身上。 我被卡在C ++编译时字符串中。 我决定采取显然无法使用的方法:使用template <char...>类。 这是我想出来的,这是非常普遍的,没有什么特别的,也是行不通的。 template <char... chars> class string { public: static constexpr const char value[] = {chars...}; constexpr string() { } constexpr operator decltype(value) & () const
This page says that the make_optional function in C++17 returns a constexpr optional<...> . I think (I might be wrong though) this would require that optional<T> has a constexpr copy or move constructor. However, this page also says that's not the case. I don't know how make_optional can be implemented as the C++1z draft currently stands. See this post for clarification.
该页面说C ++ 17中的make_optional函数返回一个constexpr optional<...> 。 我认为(我可能是错误的),这将要求optional<T>有一个constexpr复制或移动构造函数。 但是,这个页面也表示情况并非如此。 我不知道make_optional是如何实现的,因为C ++ 1z草案目前是这样。 看到这个帖子的澄清。 有没有解决方法,或者只是标准草案/ cppreference的错误? 感谢@Yakk和@TC的解释。 我觉得一个例子应该让事情更清