Date: Tue, 30 Jul 2024 20:29:26 +0400
In C++ programmers often want to hide, where a type comes from and want to create a specific type.In your proposal: int a = 4; if (cond1) a = globalVar1<double>; // 1if (cond2) a = globalVar2<float>; // 2That's a great example, Observe that before you use a you declared it as int, that means its effective type is the same as its apparent type, so no confusion here.And when you assign to it the value of globalVar1 or globalVar2, you are just reading the value of these globals only. You are not doing a variable definition.remember my previous answers. When you do:auto a = globalVar1<double>;You are preserving the effective type. But when you doint a = globalVar1<double>;You are explicitly saying make the apparent type of a the same as its effective type and let it be int, you are casting the effective type of globalVar1 to int. The effective type is assigned at the definition site.Now you also cannot tell, where a is coming from, what its original type was.The value comes from one of the globals, but its type is fixed as int, as you declared it before you use it. The language can be much more complicated, with loops and function calls. Why not insert the origin type you want into a custom type. Create a new templated class, which implicitly converts to int.As long as it is not converted, you can retrieve the origin type.But you also can use it, wherever you would need an int.But you cannot use that class in a container unless you fix its template type. Which can be avoided with my approach.Overall, it is a great contribution Sebastian.
Received on 2024-07-30 16:29:37