constants

'non-static reference member, can't use default assignment operator'

别说谁变了你拦得住时间么 提交于 2021-01-22 12:13:49
问题 I get this error when i try to compile my code: non-static reference member ‘Timestep& Timestep::previousTimestep’, can’t use default assignment operator I create one Problem which creates a Timestep a reference to the this Timestep should be stored in the vector solution . Besides i want to store a reference to a previous Timestep - and for the first Timestep that would be a reference to itself... I read that i need to define an own operator if i have const members in a class what i try to

'non-static reference member, can't use default assignment operator'

只愿长相守 提交于 2021-01-22 12:13:29
问题 I get this error when i try to compile my code: non-static reference member ‘Timestep& Timestep::previousTimestep’, can’t use default assignment operator I create one Problem which creates a Timestep a reference to the this Timestep should be stored in the vector solution . Besides i want to store a reference to a previous Timestep - and for the first Timestep that would be a reference to itself... I read that i need to define an own operator if i have const members in a class what i try to

'non-static reference member, can't use default assignment operator'

生来就可爱ヽ(ⅴ<●) 提交于 2021-01-22 12:13:06
问题 I get this error when i try to compile my code: non-static reference member ‘Timestep& Timestep::previousTimestep’, can’t use default assignment operator I create one Problem which creates a Timestep a reference to the this Timestep should be stored in the vector solution . Besides i want to store a reference to a previous Timestep - and for the first Timestep that would be a reference to itself... I read that i need to define an own operator if i have const members in a class what i try to

GCC optimization bug on weak const variable

陌路散爱 提交于 2021-01-22 06:51:11
问题 I'm getting a weird gcc behaviour when dealing with weak const variables on different optimize levels (i.e. -O0 or -O1 ). Here is the code: def.h : declarations const int var; int copy; int do_copy(void); weak.c : weak var definition, do_copy implementation doing copy = var #include "def.h" const int __attribute__((weak)) var = 1; int do_copy(void) { copy = var; return var; } main.c : strong var definition, and use of do_copy #include <stdio.h> #include "def.h" int copy = 0; int copy2 = 0;

Is it safe to make a const reference member to a temporary variable?

此生再无相见时 提交于 2021-01-18 19:14:52
问题 I've tried to code like this several times: struct Foo { double const& f; Foo(double const& fx) : f(fx) { printf("%f %f\n", fx, this->f); // 125 125 } double GetF() const { return f; } }; int main() { Foo p(123.0 + 2.0); printf("%f\n", p.GetF()); // 0 return 0; } But it doesn't crash at all. I've also used valgrind to test the program but no error or warning occured. So, I assume that the compiler automatically generated a code directing the reference to another hidden variable. But I'm

Is it safe to make a const reference member to a temporary variable?

人走茶凉 提交于 2021-01-18 19:14:22
问题 I've tried to code like this several times: struct Foo { double const& f; Foo(double const& fx) : f(fx) { printf("%f %f\n", fx, this->f); // 125 125 } double GetF() const { return f; } }; int main() { Foo p(123.0 + 2.0); printf("%f\n", p.GetF()); // 0 return 0; } But it doesn't crash at all. I've also used valgrind to test the program but no error or warning occured. So, I assume that the compiler automatically generated a code directing the reference to another hidden variable. But I'm

Why can't a Type be used as a constant value?

泪湿孤枕 提交于 2021-01-18 07:42:10
问题 Quoting MSDN - const (C# reference): A constant expression is an expression that can be fully evaluated at compile time. Therefore, the only possible values for constants of reference types are string and a null reference. According to: typeof(T) vs. Object.GetType() performance, typeof(T) is a compile time expression. So why can't a Type be a constant value? The following code will not compile: public const Type INT_TYPE = typeof(int); 回答1: Constants are substituted by the compiler with

Const arrays in C

醉酒当歌 提交于 2020-12-29 08:54:26
问题 Original question: If I define: const int z[5] = {10, 11, 12, 13, 14}; does it mean: it's a constant array of integers i.e. the address which z points to is always constant and can never change, but the elements of z can change. OR Each element of z is a constant i.e. their value can never change. Edit: More info: There is another variable: const int *y = z; func((int *) y); where func is defined as: void func(int y[]) { int i; for(i = 0; i < 5; i++) { y[i] = i; //y[i] can be set to any

Const arrays in C

我的梦境 提交于 2020-12-29 08:53:10
问题 Original question: If I define: const int z[5] = {10, 11, 12, 13, 14}; does it mean: it's a constant array of integers i.e. the address which z points to is always constant and can never change, but the elements of z can change. OR Each element of z is a constant i.e. their value can never change. Edit: More info: There is another variable: const int *y = z; func((int *) y); where func is defined as: void func(int y[]) { int i; for(i = 0; i < 5; i++) { y[i] = i; //y[i] can be set to any

What do we need std::as_const() for?

你离开我真会死。 提交于 2020-12-29 08:52:30
问题 C++11 has given us std::add_const; with C++17, we have a new structure - std::as_const(). The former just tacks a const before the type you provide it with. The second one is a proper (template of a) function, not type trait, which seems to do the same - except for when the type is an rvalue-reference, in which case it cannot be used. I don't quite understand the motivation for providing std::as_const() . Why do we need it in addition to std::add_const ? 回答1: "Need" is a strong word... std: