const

Are temporary objects in C++ const indeed?

淺唱寂寞╮ 提交于 2019-12-09 00:54:03
问题 I always believed that temporary objects in C++ are automatically considered as const by the compiler. But recently I experienced that the following example of code: function_returning_object().some_non_const_method(); is valid for C++ compiler. And it makes me wonder - are temporary objects in C++ const indeed? If yes, then why the code above is considered correct by the compiler? 回答1: No, they're not. Not unless you declare the return type as const. 回答2: It depends. int f(); const int g();

Redux 中间件与函数式编程

送分小仙女□ 提交于 2019-12-08 21:48:24
为什么需要中间件 接触过 Express 的同学对“中间件”这个名词应该并不陌生。在 Express 中,中间件就是一些用于定制对特定请求的处理过程的函数。作为中间件的函数是相互独立的,可以提供诸如记录日志、返回特定响应报头、压缩等操作。 同样的,在 Redux 中,action 对象对应于 Express 中的客户端请求,会被 Store 中的中间件依次处理。如下图所示: 中间件可以实现通用逻辑的重用,通过组合不同中间件可以完成复杂功能。它具有下面特点: 中间件是独立的函数 中间件可以组合使用 中间件有一个统一的接口 这里采用了 AOP (面向切面编程)的思想。 对于面向对象思想

Wrong function prototype used by compiler?

强颜欢笑 提交于 2019-12-08 21:43:03
问题 I faced a compilation problem that I do not understand, I have simplified it a bit for explanation below. Basically, it involves having 2 different getters (a const and non-const one) that return a container (a map in this example) with const, respectively non-const value_type. What puzzles me is that in the example below, the compiler seems unable to use the const getter on a non-const object: #include "stdafx.h" #include <utility> #include <map> class TestObject { public: TestObject() {}

Decimal.MinValue & Decimal.MaxValue: why static readonly and not const modifiers?

扶醉桌前 提交于 2019-12-08 17:45:21
问题 In C#, MinValue field is defined for numeric types with: ① static readonly modifiers for decimal type (Link to MSDN Libray for .NET 4.5): public static readonly decimal MinValue ② const modifier for all other numeric types: //Integral signed numeric types public const sbyte MinValue public const short MinValue public const int MinValue public const long MinValue //Integral unsigned numeric types public const byte MinValue public const ushort MinValue public const uint MinValue public const

why a const object of an empty class cant be created

删除回忆录丶 提交于 2019-12-08 16:39:01
问题 #include <iostream> class A { public: void foo() const { std::cout << "const version of foo" << std::endl; } void foo() { std::cout << "none const version of foo" << std::endl; } }; int main() { A a; const A ac; a.foo(); ac.foo(); } The above code can't be compiled, could anyone of you tell me why? 回答1: You need to initialize it. This is a known problem with the spec. 回答2: Initialize it as: const A ac = A(); Working code : http://www.ideone.com/SYPO9 BTW, this is not initializaiton : const A

Should I always replace 'const int' with 'constexpr int' in C++11 whenever possible?

随声附和 提交于 2019-12-08 16:33:35
问题 Would you replace const int one = 1; const int two = 2; with this? constexpr int one = 1; constexpr int two = 2; Is my understanding correct that both blocks are semantically identical and that it is currently merely a matter of taste? On the other hand, as constexpr implies const , you could argue that it is more consistent to always prefer the more restrictive form, even in trivial situations where it does not make a difference? (I understand that the situation completely changes when the

C++ return type when I don't know if it's temporary

拥有回忆 提交于 2019-12-08 16:22:21
问题 Suppose that Foo is a rather large data structure. How should I write a const virtual function that returns an instance of Foo , if I don't know whether the inherited classes will store the instance of Foo internally; thus, allowing a return by reference. If I can't store it internally, my understanding is I can't return a const reference to it because it will be a temporary. Is this correct? The two options are: virtual Foo foo() const { ... } virtual Foo const & foo() const { ... } Here's a

Static struct in C++

拈花ヽ惹草 提交于 2019-12-08 15:41:31
问题 I want to define an structure, where some math constants would be stored. Here what I've got now: struct consts { //salt density kg/m3 static const double gamma; }; const double consts::gamma = 2350; It works fine, but there would be more than 10 floating point constants, so I doesn't want to wrote 'static const' before each of them. And define something like that: static const struct consts { //salt density kg/m3 double gamma; }; const double consts::gamma = 2350; It look fine, but I got

Are compilers not allowed to assume const-ref parameters will stay const?

纵饮孤独 提交于 2019-12-08 15:40:17
问题 For example: https://godbolt.org/g/5eUkrx void f(const int&); void g1() { const int i = 42; if (i == 42) f(i); if (i == 42) f(i); } void g2() { int i = 42; if (i == 42) f(i); if (i == 42) f(i); } It seems like "f" mutating its argument should be UB, and therefore compilers should be allowed to assume it doesn't happen and optimize accordingly. Yet these two functions will produce different assembly. I don't have a copy of the standard. Is this not guaranteed? 回答1: It's perfectly fine

Constant value changing

半世苍凉 提交于 2019-12-08 15:27:07
问题 Suppose I have a struct definition: struct thing { thing* x; int z; thing() : x(this), z(0) {} void foo() const { this->x->z++; } }; Note that I create a mutable pointer to myself (evil laugh) And then I can use this later like this: int main() { const thing c; c.foo(); assert(c.z == 1); c.foo(); assert(c.z == 2); return c.z; } And as you can see it seems that I can change a constant value......is this UB? 回答1: [dcl.type.cv]p4: Except that any class member declared mutable ([dcl.stc]) can be

工具导航Map

JSON相关