[英]C++11 auto. convert from float to long
Is it possible to convert foo from float to long (and vice versa)? 是否可以将foo从float转换为long(反之亦然)?
auto foo = float(1234567891234.1234);
cout << "foo: " << foo << endl;
foo = long(1234567891234.1234);
cout << "foo: " << foo << endl;
The output is always: 输出始终为:
foo: 1.23457e+12 foo: 1.23457e+12
Not in the way you wrote it. 并非以您编写的方式。 First,
第一,
auto foo = float(1234567891234.1234);
uses auto type deduction rules to infer the type of the RHS, and the result is float
. 使用自动类型推导规则来推断RHS的类型,结果为
float
。 Once this is done, the type of foo
is float
and it is set in stone (C++ is statically typed , unlike eg Python). 完成此操作后,
foo
的类型为float
并设置为石头(C ++是静态类型的 ,与Python不同)。 When you next write 下次写时
foo = long(1234567891234.1234);
the type of foo is still float
and it is not magically changed to long
. foo的类型仍然是
float
,并且没有神奇地更改为long
。
If you want to emulate a "change" of type you can at most perform a cast: 如果要模拟类型的“更改”,则最多可以执行强制转换:
cout << "foo (as long): " << static_cast<long>(foo) << endl;
or use an additional variable 或使用其他变量
long foo_long = foo; // again you may have a loss of precision
but be aware of possible precision loss due to floating point representation. 但请注意由于浮点表示法可能导致的精度损失。
If you have access to a C++17 compiler, you can use an std::variant<long, float>
, which is a type-safe union, to switch between types. 如果可以访问C ++ 17编译器,则可以使用
std::variant<long, float>
(是类型安全的联合)在类型之间进行切换。 If not, you can just use a plain old union like 如果没有,您可以使用简单的旧联合,例如
#include <iostream>
union Foo
{
float f;
long l;
};
int main()
{
Foo foo;
foo.f = float(1234567891234.1234); // we set up the float member
std::cout << "foo: " << foo.f << std::endl;
foo.l = long(1234567891234.1234); // we set up the long member
std::cout << "foo: " << foo.l << std::endl;
}
Or, you can use a type-erasure technique like 或者,您可以使用类似类型的擦除技术
#include <iostream>
int main()
{
void *foo; // we will store the object via this pointer
foo = new int{42};
std::cout << *(int*)foo << '\n';
operator delete(foo); // don't do delete foo, it is undefined behaviour
foo = new float{42.42};
std::cout << *(float*)foo << '\n';
operator delete(foo); // don't do delete foo, it is undefined behaviour
}
The modern version of the code above can be re-written with a std::shared_ptr
like 上面代码的现代版本可以使用
std::shared_ptr
例如
#include <iostream>
#include <memory>
int main()
{
std::shared_ptr<void> foo{new int{42}};
std::cout << *(int*)foo.get() << '\n';
foo.reset(new float{42.42});
std::cout << *(float*)foo.get() << '\n';
}
A std::unique_ptr<void>
won't work as only std::shared_ptr
implements type-erasure. std::unique_ptr<void>
将不起作用,因为只有std::shared_ptr
实现类型擦除。
Of course, if you don't really care about storage size etc, just use 2 separate variables. 当然,如果您不太在意存储大小等,只需使用2个单独的变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.